Search code examples
mysqlplayframeworkebean

Playframework 2.5 [Java]: I can't insert one column data to my database


I'm using MySQL with Play, when I use Ebean to save data, all the data seems to be there except one column.

The following is my account entity:

    @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;
}

Also, this is my read method:

    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);
        }       
    }

The following is my MySql data: enter image description here

I use postman to test the read interface,and also input company_id data,but it was not successgful. How could I resolve this?


Solution

  • edit

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

    so adding the "referencedColumnName" to refer to the column in the company table conclusively solved this issue for me (tested) (you should also have the @OneToMany annotation as described below).

    old answer:

    I think you need to use a join column for the ManyToOne relationship,

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

    edit:

    You probably also need something like this in your Company class (could you post your existing code for it?):

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