我目前正在将我的应用程序的播放从2.4.x升级到2.5.18。通过play的doc,我还将play-ebean从1.0.0升级到3.2.0,使用ebean 10.4.x.升级后,每当我将bean插入h2数据库时,都会出现此错误:
[DataIntegrityException: Error[NULL not allowed for column "ID"; SQL statement: insert into users (email, name, first_name, last_name, last_login, active, email_validated) values (?,?,?,?,?,?,?) [23502-185]]]
似乎ebean没有为新插入的bean生成id。在保存(user.id = 123
)之前手动设置id时,我不会遇到错误。但是,我真的希望id生成再次工作。
版本:
在plugins.sbt
中 addSbtPlugin("com.typesafe.sbt" % "sbt-play-ebean" % "3.2.0")
在build.sbt
中lazy val root = (project in file(".")).enablePlugins(PlayJava, PlayEbean).settings(libraryDependencies ++= appDependencies)
libraryDependencies ++= Seq(
javaCore,
javaJdbc,
cache,
evolutions,
javaWs
)
在application.conf中
ebean.default = ["models.*"]
这是存储用户信息的模型。它还实现了deadbolt的主题。我四重检查了我在模型中没有超越保存。
package models;
...
@Entity
@Table(name = "users")
public class User extends AppModel implements Subject {
private static final long serialVersionUID = 1L;
@Id
public Long id;
@Constraints.Email
public String email;
public String name;
public String firstName;
public String lastName;
@Formats.DateTime(pattern = "yyyy-MM-dd HH:mm:ss")
public Date lastLogin;
public boolean active;
public boolean emailValidated;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
public List<Mapping> mappings;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
public List<DbConn> dbconns;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
public List<OntoFile> ontofiles;
@ManyToMany
public List<SecurityRole> roles;
@OneToMany(cascade = CascadeType.ALL)
public List<LinkedAccount> linkedAccounts;
@ManyToMany
public List<UserPermission> permissions;
public static final Finder<Long, User> find = new Finder<Long, User>(User.class);
...
public static User create(final AuthUser authUser) {
final User user = new User();
user.roles = Collections.singletonList(SecurityRole
.findByRoleName(controllers.Application.USER_ROLE));
user.active = true;
user.lastLogin = new Date();
user.linkedAccounts = Collections.singletonList(LinkedAccount
.create(authUser));
if (authUser instanceof EmailIdentity) {
final EmailIdentity identity = (EmailIdentity) authUser;
user.email = identity.getEmail();
user.emailValidated = false;
}
if (authUser instanceof NameIdentity) {
final NameIdentity identity = (NameIdentity) authUser;
final String name = identity.getName();
if (name != null) {
user.name = name;
}
}
if (authUser instanceof FirstLastNameIdentity) {
final FirstLastNameIdentity identity = (FirstLastNameIdentity) authUser;
final String firstName = identity.getFirstName();
final String lastName = identity.getLastName();
if (firstName != null) {
user.firstName = firstName;
}
if (lastName != null) {
user.lastName = lastName;
}
}
user.save(); // error happens here
Ebean.save(user); // this does not work either
return user;
}
...
}
我不知道为什么会这样,但是之前写这个应用程序的人把它放在那里并且它从来没有引起任何问题,所以我现在就把它留在那里。此外,将User扩展到Model而不是AppModel不会修复错误。
package models;
import javax.persistence.MappedSuperclass;
import io.ebean.Model;
@MappedSuperclass
public class AppModel extends Model
{}
我也尝试过配置ebean服务器。 id生成器和包都是空的。我将模型添加到列表中但仍然出现错误。这是否意味着ebean配置不正确?
package models;
...
public class MyServerConfigStartup implements ServerConfigStartup {
public void onStart(ServerConfig serverConfig) {
List<IdGenerator> gen = serverConfig.getIdGenerators();
System.out.println(gen); // []
List<String> pack1 = serverConfig.getPackages();
System.out.println(pack1); // []
ArrayList<String> packages = new ArrayList<String>();
packages.add("models.*");
serverConfig.setPackages(packages);
List<String> pack2 = serverConfig.getPackages();
System.out.println(pack2); // [models.*]
}
}
感谢任何帮助。谢谢。
答案 0 :(得分:0)
事实证明,h2数据库中的id列现在需要auto_increment。在所有表上运行ALTER TABLE name ALTER COLUMN colName bigint not null auto_increment
可以解决此问题。