为什么 @Size 验证注释会影响 DataNucleus JPA 数据库模式?
Why does a @Size validation annotation influence DataNucleus JPA database schema?
同时使用 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<>();
导致 @Size
的 max
值作为 LONG VARCHAR FOR BIT DATA
的长度属性传递,这在 Apache Derby 中是无效的 SQL,因此导致
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
这可能是 JPA 或 DataNucleus 中的一个难以理解的功能吗?如果是的话,我如何映射 JPA 中的字段并指定验证约束(如果可能,不涉及 XML 配置文件) ?或者这是 DataNucleus 中的错误?
我正在使用
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
@Column
中的 columnDefinition
被简单地忽略了。应该不是这样吧?
我指的是 DataNucleus JPA(与 JDO 相对)。
可以在 https://gitlab.com/krichter/datanucleus-validation-schema-confusion 找到 SSCCE。
这是因为 DataNucleus 支持将 javax.validation @Size(max="...")
注释处理为等同于 @Column(length="...")
,如 their docs state。
至于你的问题背后的原因,那需要调试。
@Size
应该只作为字符串类型 field/property 的扩展处理(我用它来做什么),所以也许没有检查它,它只是适用它与你的领域无关?
也许 JDBC 驱动程序没有将特定的 JDBC/SQL 类型报告为不允许大小?如果是这种情况,那么关于 JDBC 驱动程序的错误报告将是可行的方法。
此外,虽然此功能对很多人来说很有意义(我已经使用过),但对于那些想要 "strict JPA" 架构处理(您认为显而易见,主要用例对所有人来说都不相同,因此任何 JPA 提供者的好坏取决于它收到的请求和它提供的灵活性)。粗略地浏览一下代码,就会发现 https://github.com/datanucleus/datanucleus-core/blob/master/src/main/java/org/datanucleus/metadata/annotations/AnnotationManagerImpl.java#L101 是启用它的地方,那么为什么不提供一种将其关闭的方法呢?
同时使用 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<>();
导致 @Size
的 max
值作为 LONG VARCHAR FOR BIT DATA
的长度属性传递,这在 Apache Derby 中是无效的 SQL,因此导致
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
这可能是 JPA 或 DataNucleus 中的一个难以理解的功能吗?如果是的话,我如何映射 JPA 中的字段并指定验证约束(如果可能,不涉及 XML 配置文件) ?或者这是 DataNucleus 中的错误?
我正在使用
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
@Column
中的 columnDefinition
被简单地忽略了。应该不是这样吧?
我指的是 DataNucleus JPA(与 JDO 相对)。
可以在 https://gitlab.com/krichter/datanucleus-validation-schema-confusion 找到 SSCCE。
这是因为 DataNucleus 支持将 javax.validation @Size(max="...")
注释处理为等同于 @Column(length="...")
,如 their docs state。
至于你的问题背后的原因,那需要调试。
@Size
应该只作为字符串类型 field/property 的扩展处理(我用它来做什么),所以也许没有检查它,它只是适用它与你的领域无关?
也许 JDBC 驱动程序没有将特定的 JDBC/SQL 类型报告为不允许大小?如果是这种情况,那么关于 JDBC 驱动程序的错误报告将是可行的方法。
此外,虽然此功能对很多人来说很有意义(我已经使用过),但对于那些想要 "strict JPA" 架构处理(您认为显而易见,主要用例对所有人来说都不相同,因此任何 JPA 提供者的好坏取决于它收到的请求和它提供的灵活性)。粗略地浏览一下代码,就会发现 https://github.com/datanucleus/datanucleus-core/blob/master/src/main/java/org/datanucleus/metadata/annotations/AnnotationManagerImpl.java#L101 是启用它的地方,那么为什么不提供一种将其关闭的方法呢?