首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Playframework 2.5 [Java]:我不能在数据库中插入一列数据

Playframework 2.5 [Java]:我不能在数据库中插入一列数据
EN

Stack Overflow用户
提问于 2018-09-25 15:58:02
回答 1查看 45关注 0票数 1

我正在使用MySQL和Play,当我使用Ebean保存数据时,除了一列之外,所有的数据似乎都在那里。

下面是我的account实体:

    @Entity
public class Account extends Model {
    public static final int SUPER_ADMIN_COMPANY = -1;   
    @Id
    @Column(length=16)
    public String id;

    @Constraints.MinLength(5)
    @Constraints.MaxLength(100)
    //@Constraints.Required
    @Column(nullable=false, length=100,unique=true)
    public String username;

    /**password is not required if type is not 0*/
    @JsonIgnore
    //@Column(nullable=false)
    public String password;

    public String nicname;

    public String mobile;

    public String email;

    @ManyToOne
    public Company company;

    @Index
    @JsonIgnore
    @Column(nullable=false)
    public Date createTime;

    @Column(nullable=false)
    public Integer roleType; // 0 = super admin, 1 = admin, 2 = user

    @Column(nullable=false)
    public Boolean isEnabled = true;

    public static Find<String, Account> finder = 
            new Find<String, Account>(){};

    public static final int E_SUPERADMIN = 0;
    public static final int E_ADMIN      = 1;
    public static final int E_USER       = 2;
}

另外,这是我的read方法:

    public Result create() {
        Form<Account> form = formFactory.form(Account.class);

        try {
            String userId = session("userId");
            Account adminAccount = Account.finder.byId(userId);

            if (adminAccount == null) {
                throw new CodeException(ErrDefinition.E_ACCOUNT_INCORRECT_PARAM);               
            }

            if (adminAccount.roleType != 0 && adminAccount.roleType !=1) {
                throw new CodeException(ErrDefinition.E_ACCOUNT_UNAUTHENTICATED);               
            }

            //TODO
            /*
            if (!Authority.hasAccessRight(authority.accessRight, Authority.E_AUTHORITY_MENU)) {
                throw new CodeException(ErrDefinition.E_ACCOUNT_UNAUTHENTICATED);               
            }
            */

            if (form.hasErrors()) {     
                throw new CodeException(ErrDefinition.E_ACCOUNT_INCORRECT_PARAM);
            }

            Account newAccount = form.bindFromRequest().get();
            if (Account.finder.where().eq("username", newAccount.username).findRowCount() != 0) {
                throw new CodeException(ErrDefinition.E_ACCOUNT_ALREADY_EXIST);
            }

            if (newAccount.password == null || newAccount.password.isEmpty()) {
                throw new CodeException(ErrDefinition.E_ACCOUNT_NO_PASSWORD);
            }

            if (newAccount.password != null && !newAccount.password.isEmpty()) {
                newAccount.password = CodeGenerator.generateMD5(newAccount.password);               
            }

            newAccount.id = CodeGenerator.generateShortUUId();
            newAccount.createTime = new Date();

//          if (newAccount.roleType < 0 || newAccount.roleType > 2) {
//              throw new CodeException(ErrDefinition.E_ACCOUNT_INCORRECT_PARAM);               
//          }
            if (adminAccount.roleType == 0) {
                if (newAccount.roleType == 1) {
                    newAccount.roleType = 1;
                }
                else{
                    newAccount.roleType = 2;
                }
            }
            else{
                newAccount.roleType = 2;
            }

            newAccount.isEnabled = true;

            Ebean.save(newAccount);

            return success("id", newAccount.id);
        }
        catch (CodeException ce) {
            Logger.error(ce.getMessage());
            return failure(ce.getCode());
        }
        catch (Throwable e) {
            e.printStackTrace();
            Logger.error(e.getMessage());
            return failure(ErrDefinition.E_ACCOUNT_CREATE_FAILED);
        }       
    }

以下是我的MySql数据:enter image description here

我使用postman测试了读取接口,还输入了company_id数据,但没有成功。我该如何解决这个问题呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-26 08:57:23

编辑

@ManyToOne
@JoinColumn(name = "company_id", referencedColumnName = "company_id")
private Company company;

因此,添加"referencedColumnName“来引用company表中的列,最终为我解决了这个问题(已测试)(您还应该有@OneToMany注释,如下所述)。

老生常谈:

我认为您需要对ManyToOne关系使用连接列,

@ManyToOne
@JoinColumn(name = "company_id")
public Company company;

编辑:

在你的Company类中,你可能也需要这样的东西(你能发布你现有的代码吗?):

@OneToMany(mappedBy = "company")
private List<Accounts> accounts = new Arraylist<>();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52493288

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档