我正在使用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
数据,但没有成功。我该如何解决这个问题呢?
发布于 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<>();
https://stackoverflow.com/questions/52493288
复制相似问题