添加单独的persistenceUnit仍然会查看broadleaf自己的实体并失败

时间:2014-01-05 16:43:31

标签: hibernate persistence-unit broadleaf-commerce

我跟着this tutorial并开始添加另一个指向单独数据库的持久性单元。它不应该共享阔叶自己的实体。所以......

核心项目现在拥有带有这些新bean的applicationContext.xml:

<!-- New data source to our DB -->

<bean id="blMergedDataSources" class="org.springframework.beans.factory.config.MapFactoryBean">
    <property name="sourceMap">
        <map>
            <entry key="jdbc/in1" value-ref="in1DS" />
        </map>
    </property>
</bean>

<bean id="entityManagerFactoryIn1PU"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="jpaVendorAdapter" ref="blJpaVendorAdapter" />
    <property name="persistenceUnitManager" ref="blPersistenceUnitManager" />
    <property name="persistenceUnitName" value="in1PU" />
</bean>

<bean id="blPersistenceUnitManager" class="org.broadleafcommerce.common.extensibility.jpa.MergePersistenceUnitManager">
    <property name="persistenceXmlLocations">
        <list>
            <value>classpath*:/META-INF/in1_persistence.xml</value>
        </list>
    </property>
    <property name="persistenceUnitPostProcessors">
        <list>
            <bean
                class="org.broadleafcommerce.common.extensibility.jpa.JPAPropertiesPersistenceUnitPostProcessor">
                <property name="persistenceUnitProperties">
                    <map>
                        <entry key="in1PU.hibernate.dialect" value="${in1PU.hibernate.dialect}" />
                        <entry key="in1PU.hibernate.hbm2ddl.auto" value="${in1PU.hibernate.hbm2ddl.auto}"/>
                        <entry key="in1PU.hibernate.show_sql" value="${in1PU.hibernate.show_sql}"/>
                        <entry key="in1PU.hibernate.cache.use_second_level_cache" value="${in1PU.hibernate.cache.use_second_level_cache}"/>
                        <entry key="in1PU.hibernate.cache.use_query_cache" value="${in1PU.hibernate.cache.use_query_cache}"/>
                        <entry key="in1PU.hibernate.hbm2ddl.import_files" value="${in1PU.hibernate.hbm2ddl.import_files}"/>
                        <entry key="in1PU.hibernate.hbm2ddl.import_files_sql_extractor" value="${in1PU.hibernate.hbm2ddl.import_files_sql_extractor}"/>
                    </map>
                </property>
            </bean>
        </list>
    </property>
</bean>

核心项目还有文件/META-INF/in1_persistance.xml,其中包含以下内容:

<persistence-unit name="in1PU" transaction-type="RESOURCE_LOCAL">
    <non-jta-data-source>jdbc/in1</non-jta-data-source>    
    <exclude-unlisted-classes/>
</persistence-unit>

此持久性单元的属性与blPU属性非常相似,例如:

in1PU.hibernate.hbm2ddl.auto=create-drop
in1PU.hibernate.dialect=com.in1.hibernate.SubSQLServer2008Dialect

在admin中,新资源被添加到\ admin \ src \ main \ webapp \ META-INF \ context.xml:

<Resource name="jdbc/in1" [....] />

同样在admin中,新的jndi资源在\ admin \ src \ main \ webapp \ WEB-INF \ jetty-env.xml中设置:

<New id="in1DS" class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg>jdbc/in1</Arg>
    <Arg>
        <New class="org.apache.commons.dbcp.BasicDataSource">
            <Set name="driverClassName">net.sourceforge.jtds.jdbc.Driver</Set>
            <Set name="url">jdbc:jtds:sqlserver://localhost:1433;databaseName=DB2</Set>
            <Set name="username">xyz</Set>
            <Set name="password">xyz</Set>
        </New>
    </Arg>
</New> 

对于applicationContext-datasource.xml,添加了以下<jee:jndi-lookup id="in1DS" jndi-name="jdbc/in1"/>

应用程序启动并失败,并显示以下错误消息:

[artifact:mvn] javax.persistence.PersistenceException: [PersistenceUnit: in1PU] Unable to build EntityManagerFactory
[...]
[artifact:mvn] Caused by: 
[artifact:mvn] org.hibernate.AnnotationException: @OneToOne or @ManyToOne on org.broadleafcommerce.core.catalog.domain.ProductImpl.defaultCategory references an unknown entity: org.broadleafcommerce.core.catalog.domain.CategoryImpl
[artifact:mvn]  at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:109)

我想知道为什么这个新的持久性单元会查找ProductImpl类?它没有列出使用它(在in1_persistence.xml中没有列出的类,并且<exclude-unlisted-classes/>已打开)。它肯定看不到CategoryImpl实体....我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

我在调试org.hibernate.ejb.Ejb3Configuration之后回答了我自己的问题。

在persistence.xml中,只有<exclude-unlisted-classes/>是不够的。还必须设置属性hibernate.archive.autodetection基本上不能为空。我们不需要"class""hbm""class,hbm"中的有效值,因此可以将值设置为任何值,"false"表现良好。

所以正确的最小persistence.xml是:

<persistence-unit name="in1PU" transaction-type="RESOURCE_LOCAL">
    <non-jta-data-source>jdbc/in1</non-jta-data-source>    
    <exclude-unlisted-classes/>
    <properties>
        <property name="hibernate.archive.autodetection" value="false" />
    </properties>
</persistence-unit>

希望这有帮助。