Playframework 2.5 [Java]:我无法将一列数据插入我的数据库
Playframework 2.5 [Java]: I can't insert one column data to my database
我在 Play 中使用 MySQL,当我使用 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
数据,但是没有成功。我该如何解决这个问题?
编辑
@ManyToOne
@JoinColumn(name = "company_id", referencedColumnName = "company_id")
private Company company;
所以加上"referencedColumnName"引用公司里的专栏table终于为我解决了这个问题(测试过)(你应该也有@OneToMany 注释如下所述)。
旧答案:
我认为您需要为 ManyToOne 关系使用连接列,
@ManyToOne
@JoinColumn(name = "company_id")
public Company company;
编辑:
您的公司可能也需要这样的东西 class(您可以 post 现有的代码吗?):
@OneToMany(mappedBy = "company")
private List<Accounts> accounts = new Arraylist<>();
我在 Play 中使用 MySQL,当我使用 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
数据,但是没有成功。我该如何解决这个问题?
编辑
@ManyToOne
@JoinColumn(name = "company_id", referencedColumnName = "company_id")
private Company company;
所以加上"referencedColumnName"引用公司里的专栏table终于为我解决了这个问题(测试过)(你应该也有@OneToMany 注释如下所述)。
旧答案:
我认为您需要为 ManyToOne 关系使用连接列,
@ManyToOne
@JoinColumn(name = "company_id")
public Company company;
编辑:
您的公司可能也需要这样的东西 class(您可以 post 现有的代码吗?):
@OneToMany(mappedBy = "company")
private List<Accounts> accounts = new Arraylist<>();