我的applicationContext.xml
是:
<aop:aspectj-autoproxy/>
<context:component-scan base-package="com"/>
<context:annotation-config/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<tx:jta-transaction-manager/>
<tx:annotation-driven/>
<jee:jndi-lookup id="corePU" jndi-name="java:/exampleDatasource" expected-type="javax.sql.DataSource"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:/META-INF/persistence.xml" />
<property name="persistenceUnitName" value="corePU" />
<property name="packagesToScan" value="com" />
</bean>
每个实体都有@Entity
,在persistence.xml
中设置以下属性
<exclude-unlisted-classes>false</exclude-unlisted-classes>
Hibernate version : 5.1.0.Final
Spring version : 4.1.3.RELEASE
JPA 2.0
WildFly 10
在applicationContext.xml
中,我定义了一个entityManagerFactory bean,如下所示"
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:/META-INF/persistence.xml" />
<property name="persistenceUnitName" value="corePU" />
<property name="packagesToScan" value="com" />
</bean>
persistence.xml
如下:
<persistence-unit name="corePU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:/exampleDatasource</jta-data-source>
<!--<class>com.entity.Address</class>-->
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<!--<shared-cache-mode>NONE</shared-cache-mode>-->
<properties>
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="false"/>
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform"/>
<property name="org.hibernate.envers.audit_table_prefix" value="z_"/>
<property name="hibernate.cache.use_second_level_cache" value="false" />
<property name="hibernate.generate_statistics" value="false"/>
<property name="hibernate.cache.use_query_cache" value="false"/>
<property name="hibernate.archive.autodetection" value="class, hbm" />
</properties>
</persistence-unit>
下面是我得到的错误:
21:15:47,463 ERROR [stderr] (default task-4) java.lang.IllegalArgumentException: Unknown entity: com.entity.Address
21:15:47,463 ERROR [stderr] (default task-4) at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1149)
21:15:47,464 ERROR [stderr] (default task-4) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
21:15:47,464 ERROR [stderr] (default task-4) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
21:15:47,464 ERROR [stderr] (default task-4) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
21:15:47,464 ERROR [stderr] (default task-4) at java.lang.reflect.Method.invoke(Method.java:498)
21:15:47,464 ERROR [stderr] (default task-4) at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:347)
21:15:47,464 ERROR [stderr] (default task-4) at com.sun.proxy.$Proxy93.persist(Unknown Source)
21:15:47,464 ERROR [stderr] (default task-4) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
21:15:47,464 ERROR [stderr] (default task-4) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
21:15:47,464 ERROR [stderr] (default task-4) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
21:15:47,464 ERROR [stderr] (default task-4) at java.lang.reflect.Method.invoke(Method.java:498)
21:15:47,465 ERROR [stderr] (default task-4) at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:298)
21:15:47,465 ERROR [stderr] (default task-4) at com.sun.proxy.$Proxy50.persist(Unknown Source)
21:15:47,465 ERROR [stderr] (default task-4) at com.manager.manager.getAddress(manager.java:37)
21:15:47,465 ERROR [stderr] (default task-4) at com.manager.manager$$FastClassBySpringCGLIB$$e766fff.invoke(<generated>)
21:15:47,465 ERROR [stderr] (default task-4) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
我如何使用我的实体没有指定每个实体明确在persistence.xml
与class
标签?
JPA和Spring是两个不同的东西。
一般来说,Spring的<context:component-scan base-package="..."/>
与JPA无关。它指的是Spring组件、箱子等。但是在底层,当然,Spring使用JPA,并且有两种可能的配置:
- 配置Spring 读取persistence.xml并完全依赖它;
- 不要使用persistence.xml,只使用Spring配置——另一种类型的配置,你可以使用
LocalContainerEntityManagerFactoryBean
属性packagesToScan
。请看这篇文章,例如:https://stackoverflow.com/a/16088340/2816631.
检查Spring javadoc关于packagesToScan
的说明:
设置是否对实体类使用基于spring的扫描类路径,而不是使用JPA对jar文件的标准扫描其中包含Persistence.xml标记。对于基于spring的扫描,不需要Persistence.xml是必需的;你所需要做的就是指定底
如果您想使用persistence.xml,您有几个选项:
- 写每个实体类——JPA不支持自动扫描(好吧,几乎,见下一个选项);
- 不编写每个实体类,而是将实体放在一个jar中,并设置
jar-file
属性指向它——JPA只支持这种自动扫描; - 如果你不介意不符合JPA规范(即使用JPA本身没有的特定提供者功能),你可以使用Hibernate自动扫描功能:
Hibernate自动扫描特性:
<persistence-unit name="unitName" transaction-type="RESOURCE_LOCAL">
<!-- This is required to be spec compliant, Hibernate however supports
auto-detection even in JSE.
<class>com.entity.User</class>
<class>com.entity.Address</class>
...
-->
<properties>
<!-- Scan for annotated classes and Hibernate mapping XML files -->
<property name="hibernate.archive.autodetection" value="class, hbm"/>
...
</properties>
</persistence-unit>
您应该在entityManager
/sessionFactory
配置中设置packagesToScan
属性。
packagesToScan="com.manager"
或者,如果您正在使用Spring Boot,您可以使用@EntityScan
注释:
Spring Boot 1.4+
弹簧引导<1.4