从 ORM 和 OGM 获取休眠持久性单元 类
Getting hibernate persistence-unit classes from both ORM and OGM
我正在尝试在同一个应用程序中同时使用 hibernate 的 orm 和 ogm。
在我的 persistence.xml
我有两个 persistence-unit
:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="orm-mysql">
...
<class>...</class>
...
<class>...</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
...
</persistence-unit>
<persistence-unit name="ogm-mongodb" transaction-type="JTA">
...
<class>...</class>
...
<class>...</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
...
</persistence-unit>
...
</persistence>
我想将 hibernate orm native api 与 orm 和 ogm 一起使用,因此我需要将带注释的 类 添加到两个不同的 MetadataSources
.
因为我有持久性单元,所以我认为我可以这样做:
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("orm-mysql");
Set<ManagedType<?>> managedTypes = entityManagerFactory.getMetamodel().getManagedTypes();
for (ManagedType type : managedTypes) {
ormSources.addAnnotatedClass(type.getJavaType());
}
entityManagerFactory = Persistence.createEntityManagerFactory("ogm-mongo");
managedTypes = entityManagerFactory.getMetamodel().getManagedTypes();
for (ManagedType type : managedTypes) {
ogmSources.addAnnotatedClass(type.getJavaType());
}
问题是在第一次迭代中,orm 类,我得到了包含在 orm-mysql
和 ogm-mongo
中的 orm 和 ogm 类 .
不过第二次没问题,我只得到 ogm-mongo
类.
不知道哪里出了问题,有什么想法吗?
如果有人也犯了这个愚蠢的错误,我有:
<persistence-unit name="orm-mysql">
...
<class>...</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.archive.autodetection" value="class"/>
...
</properties>
</persistence-unit>
删除 hibernate.archive.autodetection
属性 解决了问题。
我正在尝试在同一个应用程序中同时使用 hibernate 的 orm 和 ogm。
在我的 persistence.xml
我有两个 persistence-unit
:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="orm-mysql">
...
<class>...</class>
...
<class>...</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
...
</persistence-unit>
<persistence-unit name="ogm-mongodb" transaction-type="JTA">
...
<class>...</class>
...
<class>...</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
...
</persistence-unit>
...
</persistence>
我想将 hibernate orm native api 与 orm 和 ogm 一起使用,因此我需要将带注释的 类 添加到两个不同的 MetadataSources
.
因为我有持久性单元,所以我认为我可以这样做:
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("orm-mysql");
Set<ManagedType<?>> managedTypes = entityManagerFactory.getMetamodel().getManagedTypes();
for (ManagedType type : managedTypes) {
ormSources.addAnnotatedClass(type.getJavaType());
}
entityManagerFactory = Persistence.createEntityManagerFactory("ogm-mongo");
managedTypes = entityManagerFactory.getMetamodel().getManagedTypes();
for (ManagedType type : managedTypes) {
ogmSources.addAnnotatedClass(type.getJavaType());
}
问题是在第一次迭代中,orm 类,我得到了包含在 orm-mysql
和 ogm-mongo
中的 orm 和 ogm 类 .
不过第二次没问题,我只得到 ogm-mongo
类.
不知道哪里出了问题,有什么想法吗?
如果有人也犯了这个愚蠢的错误,我有:
<persistence-unit name="orm-mysql">
...
<class>...</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.archive.autodetection" value="class"/>
...
</properties>
</persistence-unit>
删除 hibernate.archive.autodetection
属性 解决了问题。