除非在persistence.xml中手动指定,否则无法找到Hibernate实体

时间:2014-12-05 10:25:44

标签: java xml spring hibernate spring-boot

我有两个应用程序,

App - main app : depends on lib
lib - secondary app.

使用spring boot我正在为app生成可执行jar文件,这样lib的jar就在jar的lib文件夹中(my.jar!/lib/lib.jar)

我正在使用hibernate,具有以下配置

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" 
    depends-on="fakeJpaProperties,fakeDataSource">
    <property name="persistenceUnitName" value="fake" />
    <property name="persistenceUnitManager">
        <bean class="my.persistence.MergingPersistenceUnitManager">
            <property name="persistenceXmlLocations">
                <list>
                    <value>classpath*:META-INF/fake/persistence.xml</value>
                </list>
            </property>
        </bean>
    </property>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="false" />
        </bean>
    </property>
    <property name="packagesToScan" value="my.path.to.lib.data.card" />
</bean>

以下persistence.xml在lib

<persistence-unit name="fake" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <non-jta-data-source>fake</non-jta-data-source>
    <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
</persistence-unit>

在app中使用此persistence.xml

<persistence-unit name="fake" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <non-jta-data-source>fake</non-jta-data-source>
    <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
    <jar-file>lib/lib.jar</jar-file>
</persistence-unit>

然而我得到了:

  org.hibernate.MappingException: Unknown entity: my.path.to.lib.data.card.FakeData
    at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1096)
    at org.hibernate.internal.SessionImpl.locateEntityPersister(SessionImpl.java:2565)
    at org.hibernate.internal.SessionImpl.access$1900(SessionImpl.java:176)
    at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2504)
    at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2508)
    at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2495)
    at org.hibernate.internal.SessionImpl.byId(SessionImpl.java:1043)
    at org.hibernate.internal.SessionImpl.get(SessionImpl.java:1023)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:1104)
    ... 91 common frames omitted

如果我在lib的persistence.xml中添加:<class>my.path.to.lib.data.card.FakeData</class>,则异常不起作用。我不想手动指定所有实体,这就是我的entityManagerFactory包含"packagesToScan"的原因,但是它似乎没有被扫描。

如何让packagestoScan工作?


修改

我发现here如果存在persistence.xml,则会忽略packagesToScan。在我的情况下,由于依赖于需要它的其他几个共享项目,我无法删除所有persistence.xml。有没有办法指定包/通配符而不是列出persistence.xml中的每个<class>

2 个答案:

答案 0 :(得分:0)

  

有没有办法指定包/通配符而不是在persistence.xml中列出每个人?

AFAIK是指定JPA的方式,所以如果你使用persistence.xml,那就是你需要的东西。

答案 1 :(得分:0)

尝试将@EntityScan注释添加到spring boot配置类:

@Configuration
@EntityScan(basePackages="my.path.to.lib.data.card")
public class application {

    public static void main(String[] args) {
        SpringApplication.run(application.class, args);
    }

}