我正在使用Play 2.2.0和Ebean开发应用程序。 在通过代码直接创建实体时,我遇到了问题,而不是使用表单。让我们说这是我的实体:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import models.SuperEntity;
import com.avaje.ebean.validation.Length;
@Entity
@Table(name = "foos")
public class Foo extends SuperEntity {
@Column(unique = true)
@NotNull
@Length(min = 2, max = 2)
public String code;
}
这是我的单元测试:
@Test
public void testCreate() {
running(fakeApplication(), new Runnable() {
@Override
public void run() {
Foo foo = new Foo();
foo.code = "foo1";
foo.save();
}
});
}
问题是验证注释似乎完全被忽略了。如果我将代码设置为null,则表示数据库错误不是播放错误。
[error] Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'name' cannot be null
[error] at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[error] at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
[error] at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
[error] at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
[error] at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
[error] at com.mysql.jdbc.Util.getInstance(Util.java:386)
[error] at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1041)
[error] at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4190)
[error] at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4122)
[error] at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570)
[error] at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731)
[error] at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2818)
[error] at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2157)
[error] at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2460)
[error] at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2377)
[error] at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2361)
[error] at com.jolbox.bonecp.PreparedStatementHandle.executeUpdate(PreparedStatementHandle.java:205)
[error] at com.avaje.ebeaninternal.server.type.DataBind.executeUpdate(DataBind.java:55)
[error] at com.avaje.ebeaninternal.server.persist.dml.InsertHandler.execute(InsertHandler.java:134)
[error] at com.avaje.ebeaninternal.server.persist.dml.DmlBeanPersister.execute(DmlBeanPersister.java:86)
@Length注释也被忽略,我可以在数据库中输入我想要的长度。数据库中的列应为varchar(2),它是varchar(255)。
我该如何解决这个问题?提前谢谢。
编辑:我在@Min,@Max on Long或Integer属性等注释上遇到了相同的错误...
答案 0 :(得分:1)
Ebean不会验证保存数据。在DDL生成期间使用诸如@NotNull
和@Column(unique = true)
的验证注释来指定表约束。关于@Length
注释,Ebean可能不支持这一点,但我无法找到文档。文档肯定是Ebean的一个问题。关于Ebean与Play的未来有一个discussion on the Play git repo。它包含有关Ebean上新/活跃开发的信息。
答案 1 :(得分:0)
javax.validation
。请参阅com.avaje.ebean.validation
包,但我建议您不要使用它。