我正在使用 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,我会遇到数据库错误而不是 Play 错误。
[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)。
我怎样才能解决这个问题 ?提前致谢。
编辑:我对Long或Integer属性上的@Min,@Max等注释有同样的错误......