为什么@Size验证注释会影响DataNucleus JPA数据库架构?

时间:2018-02-04 16:37:54

标签: java validation jpa datanucleus

使用JPA和验证注释(如

)的有效实体
@Entity
public class MyEntity implements Serializable {
    private static final long serialVersionUID = 1L;
    public static final int IMAGE_COUNT_MAX = 5;
    @Id
    @GeneratedValue
    private Long id;
    @Size(max = IMAGE_COUNT_MAX, message = "Maximal " + IMAGE_COUNT_MAX + " images are allowed")
    @Basic
    @Lob
    private ArrayList<byte[]> imageData = new ArrayList<>();

导致max的{​​{1}}值作为@Size的长度属性传递,这是Apache Derby中无效的SQL,从而导致

LONG VARCHAR FOR BIT DATA

这可能是 - 难以理解 - JPA或DataNucleus中的功能 - 如果是 - 我如何映射JPA中的字段并指定验证约束(如果可能,不涉及XML配置文件)?或者这是DataNucleus中的错误吗?

我正在使用

SEVERE: Error thrown executing CREATE TABLE MYENTITY
(
    ID BIGINT NOT NULL generated always as identity (start with 1),
    IMAGEDATA LONG VARCHAR FOR BIT DATA(5)
) : Syntax error: Encountered "(" at line 4, column 40.
java.sql.SQLSyntaxErrorException: Syntax error: Encountered "(" at line 4, column 40.
    at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
    at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source)
    at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source)
    at org.datanucleus.store.rdbms.datasource.dbcp2.DelegatingStatement.execute(DelegatingStatement.java:291)
    at org.datanucleus.store.rdbms.datasource.dbcp2.DelegatingStatement.execute(DelegatingStatement.java:291)
    at org.datanucleus.store.rdbms.table.AbstractTable.executeDdlStatement(AbstractTable.java:849)
    at org.datanucleus.store.rdbms.table.AbstractTable.executeDdlStatementList(AbstractTable.java:800)
    at org.datanucleus.store.rdbms.table.AbstractTable.create(AbstractTable.java:522)
    at org.datanucleus.store.rdbms.table.AbstractTable.exists(AbstractTable.java:580)
    at org.datanucleus.store.rdbms.RDBMSStoreManager$ClassAdder.performTablesValidation(RDBMSStoreManager.java:3447)
    at org.datanucleus.store.rdbms.RDBMSStoreManager$ClassAdder.run(RDBMSStoreManager.java:2969)
    at org.datanucleus.store.rdbms.AbstractSchemaTransaction.execute(AbstractSchemaTransaction.java:118)
    at org.datanucleus.store.rdbms.RDBMSStoreManager.manageClasses(RDBMSStoreManager.java:1672)
    at org.datanucleus.store.rdbms.RDBMSStoreManager.getDatastoreClass(RDBMSStoreManager.java:648)
    at org.datanucleus.store.rdbms.RDBMSPersistenceHandler.getDatastoreClass(RDBMSPersistenceHandler.java:88)
    at org.datanucleus.store.rdbms.RDBMSPersistenceHandler.insertObject(RDBMSPersistenceHandler.java:123)
    at org.datanucleus.state.StateManagerImpl.internalMakePersistent(StateManagerImpl.java:4535)
    at org.datanucleus.state.StateManagerImpl.flush(StateManagerImpl.java:5735)
    at org.datanucleus.flush.FlushOrdered.execute(FlushOrdered.java:106)
    at org.datanucleus.ExecutionContextImpl.flushInternal(ExecutionContextImpl.java:4058)
    at org.datanucleus.ExecutionContextImpl.flush(ExecutionContextImpl.java:4004)
    at org.datanucleus.api.jpa.JPAEntityManager.flush(JPAEntityManager.java:946)
    at richtercloud.datanucleus.validation.schema.confusion.NewMain.main(NewMain.java:33)
Caused by: ERROR 42X01: Syntax error: Encountered "(" at line 4, column 40.
    at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
    at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
    at org.apache.derby.impl.sql.compile.ParserImpl.parseStatementOrSearchCondition(Unknown Source)
    at org.apache.derby.impl.sql.compile.ParserImpl.parseStatement(Unknown Source)
    at org.apache.derby.impl.sql.GenericStatement.prepMinion(Unknown Source)
    at org.apache.derby.impl.sql.GenericStatement.prepare(Unknown Source)
    at org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext.prepareInternalStatement(Unknown Source)
    ... 22 more

richtercloud:datanucleus-validation-schema-confusion:jar:1.0-SNAPSHOT +- org.datanucleus:javax.persistence:jar:2.2.0-release:compile +- org.datanucleus:datanucleus-api-jpa:jar:5.1.4:compile +- org.datanucleus:datanucleus-core:jar:5.1.4:compile +- org.datanucleus:datanucleus-rdbms:jar:5.1.4:compile +- org.apache.derby:derbyclient:jar:10.14.1.0:compile +- org.apache.derby:derby:jar:10.14.1.0:compile \- org.hibernate:hibernate-validator:jar:4.3.2.Final:compile +- javax.validation:validation-api:jar:1.0.0.GA:compile \- org.jboss.logging:jboss-logging:jar:3.1.0.CR2:compile 中的columnDefinition被忽略。情况应该不是这样,对吧?

我指的是使用DataNucleus JPA(而不是JDO)。

可以在KeyPressEventArgs找到SSCCE。

1 个答案:

答案 0 :(得分:1)

这是因为DataNucleus支持将javax.validation @Size(max="...")注释作为@Column(length="...")的等效项处理为their docs state

至于你的问题背后的原因,这需要调试。

@Size只应作为String类型字段/属性的扩展名处理(我用过它),所以也许没有检查它,它只是应用它而不管你的场?

也许JDBC驱动程序不会将特定的JDBC / SQL类型报告为不允许大小?如果是这种情况,则可以使用JDBC驱动程序的错误报告。

此外,虽然这个功能对很多人来说很有意义(我已经使用过它),但对于那些需要“严格JPA”模式处理的人来说,它是可配置的(你认为很明显和主要用例对所有人来说都不一样,所以任何JPA提供者都只能获得它所获得的请求和它提供的灵活性。最近粗略地看一下代码,https://github.com/datanucleus/datanucleus-core/blob/master/src/main/java/org/datanucleus/metadata/annotations/AnnotationManagerImpl.java#L101作为启用它的地方,那么为什么不提供关闭它的方法呢?