首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从ORM和OGM获取hibernate持久性单元类

从ORM和OGM获取hibernate持久性单元类
EN

Stack Overflow用户
提问于 2016-01-22 22:07:20
回答 1查看 141关注 0票数 0

我试图在同一个应用程序中同时使用hibernate的orm和ogm。在我的persistence.xml里我有两个persistence-unit

代码语言:javascript
复制
<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>

我希望使用带有orm和ogm的hibernate orm本机api,因此我需要将带注释的类添加到两个不同的MetadataSources中。

因为我有持久性单元,所以我认为我可以这样做:

代码语言:javascript
复制
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-mysqlogm-mongo中的orm和ogm类。

不过,第二次讨论还不错,我只获得了ogm-mongo类。

不知道出了什么问题,有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-23 23:33:15

如果有人也犯了这个愚蠢的错误,我会:

代码语言:javascript
复制
<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属性解决了这个问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34956756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档