【发布时间】:2015-12-01 11:55:07
【问题描述】:
我们在同一个项目中存在多个持久性单元的问题。我们需要同时使用两个持久化单元。一种是“正常”的,Hibernate 用于 MySQL,另一种是 Kundera 用于 Cassandra。
问题在于,与 Hibernate/MySQL 不同,Kundera/Cassandra 似乎不支持 JPA 2.1。
持久性 XML 看起来像:
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="pu-mysql" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/datasrc</jta-data-source>
<class>com.blah.Contact</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
</properties>
</persistence-unit>
<persistence-unit name="cassandra-pu">
<provider>com.impetus.kundera.KunderaPersistence</provider>
<class>com.blah.nosql.PostLogNosql</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="kundera.nodes" value="localhost"/>
<property name="kundera.port" value="9042"/>
<property name="kundera.keyspace" value="keyspace_name"/>
<property name="kundera.dialect" value="cassandra"/>
<property name="kundera.ddl.auto.prepare" value="validate"/>
<property name="kundera.client.lookup.class" value="com.impetus.kundera.client.cassandra.dsdriver.DSClientFactory"/>
<property name="kundera.cache.provider.class" value="com.impetus.kundera.cache.ehcache.EhCacheProvider"/>
<property name="kundera.annotations.scan.package" value="com.blah.nosql"/>
<property name="jboss.as.jpa.managed" value="false"/>
</properties>
</persistence-unit>
</persistence>
这不起作用。系统没有看到文件的 Kundera 部分,因为 Kundera 不支持 JPA 2.1。我们收到 org.hibernate.HibernateException: Missing column: time_key in keyspace_name.ourtable 错误。
通过将持久性标头更改为 2.0 版解决了这个问题。然后标题如下所示:
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_0.xsd">
...一切都像魅力一样。到目前为止一切顺利。
现在,问题是 Hibernate/MySQL 作为 JPA 2.1 兼容会发生什么。它的所有 JPA 2.1 特定功能是否仍然可用,还是仅限于与 JPA 2.0 相关的子集?
如果是后者,我怎么能同时使用 Hibernate/MySQL 作为 JPA 2.1 和 Kundera/Cassandra 作为 JPA 2.0?
使用的应用服务器是 Wildfly 8 和 Wildfly 9。非常感谢。
【问题讨论】:
-
您在 CLASSPATH 中有一个 JPA API jar,因此无论哪种方式都会出现问题。如果您尝试使用 JPA 2.0,那么您可能会在 Hibernate 中获得 AbstractMethodError 等,反之亦然,使用 JPA 2.1 和 Kundera。您可以选择将 DataNucleus 与 Cassandra 一起用于 JPA 2.1(它也可以使用 RDBMS FWIW)。 persistence.xml 标头是您的问题中最少的... 2.0 就足够了,因为在 JPA 2.1 中没有任何改变
-
嗨@Neil Stockton,谢谢,这应该是一个有效的答案。我们终于在别处解决了这个问题,但是,您的解释很有价值。
-
@TomS 我不认为问题是因为“Kundera 不支持 JPA 2.1”,因为我尝试使用带有持久性标头版本 2.1 的 Kundera 并且它有效!另外,您能分享一下您的问题是如何解决的吗?
标签: hibernate jakarta-ee jpa persistence kundera