playframework2.5[java]:我不能向数据库中插入一列数据

tktrz96b  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(391)

我使用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数据:在这里输入图像描述
我使用postman来测试read接口,并输入 company_id 但没有成功。我怎样才能解决这个问题?

ldxq2e6h

ldxq2e6h1#

编辑

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

因此,添加“referencedcolumnname”来引用company表中的列最终解决了这个问题(tested)(您还应该有@onetomany注解,如下所述)。
旧答案:
我想你需要用一个连接列来表示manytone关系,

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

编辑:
您可能还需要在公司类中使用类似的内容(您可以发布现有的代码吗?):

@OneToMany(mappedBy = "company")
private List<Accounts> accounts = new Arraylist<>();

相关问题