java 实体扫描在 JPA/hibernate/spring 应用程序中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39192126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Entity scanning not working in JPA/hibernate/spring application
提问by MDaniyal
My applicationContext.xml
is:
我的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>
Each entity has @Entity
and in persistence.xml
following property is set
每个实体都设置@Entity
了persistence.xml
以下属性
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
Hibernate version : 5.1.0.Final
Spring version : 4.1.3.RELEASE
JPA 2.0
WildFly 10
In applicationContext.xml
I have defined an entityManagerFactory bean as follows"
在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
is as follows:
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>
Below is the error I am getting:
以下是我收到的错误:
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)
How can I use my entities without specifying each entity explicitly in persistence.xml
with class
tag?
如何使用我的实体而不在persistence.xml
withclass
标签中明确指定每个实体?
回答by Artem Novikov
JPA and Spring are two different things.
JPA 和 Spring 是两个不同的东西。
The Spring's <context:component-scan base-package="..."/>
has nothing to do with JPA, in general. It refers to Spring components, bins, etc. But under the hood, of course, Spring uses JPA and there are two possible configurations of this:
一般来说,Spring<context:component-scan base-package="..."/>
与 JPA 无关。它指的是 Spring 组件、bins 等。但在底层,当然,Spring 使用 JPA,并且有两种可能的配置:
- configure Spring to read persistence.xmland completely rely on it;
- don't use persistence.xmland use a Spring configuration only - another type of configuration, where you could use a
LocalContainerEntityManagerFactoryBean
propertypackagesToScan
. See this post, for example: https://stackoverflow.com/a/16088340/2816631.
- 配置Spring读取persistence.xml,完全依赖它;
- 不要使用persistence.xml并只使用Spring 配置——另一种类型的配置,您可以在其中使用
LocalContainerEntityManagerFactoryBean
属性packagesToScan
。参见这篇文章,例如:https: //stackoverflow.com/a/16088340/2816631。
Check what Spring javadoc says about the packagesToScan
:
检查 Spring javadoc 是怎么说的packagesToScan
:
Set whether to use Spring-based scanning for entity classes in the classpath instead of using JPA's standard scanning of jar files with persistence.xml markers in them. In case of Spring-based scanning, no persistence.xml is necessary; all you need to do is to specify base packages to search here.
设置是否对类路径中的实体类使用基于 Spring 的扫描,而不是使用 JPA 的标准扫描其中带有 persistence.xml 标记的 jar 文件。如果是基于 Spring 的扫描,则不需要 persistence.xml;您需要做的就是在此处指定要搜索的基本包。
If you want to use persistence.xml, you have several options:
如果你想使用 persistence.xml,你有几个选择:
- write each entity class - JPA doesn't support auto-scan (well, almost, see the next option);
- don't write each entity class, but have your entities in one jar and set the
jar-file
property to point to it - JPA supports only this kind of auto-scan; - if you don't mind being not JPA specification compliant (i.e. use a specific provider feature that JPA itself doesn't have), you can use Hibernate auto-scanfeature:
- 编写每个实体类 - JPA 不支持自动扫描(好吧,几乎,请参阅下一个选项);
- 不要编写每个实体类,而是将实体放在一个 jar 中并将
jar-file
属性设置为指向它 - JPA 仅支持这种自动扫描; - 如果您不介意不符合 JPA 规范(即使用 JPA 本身没有的特定提供程序功能),您可以使用Hibernate 自动扫描功能:
Hibernate auto-scanfeature:
休眠自动扫描功能:
<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>
回答by Maciej Marczuk
You should set packagesToScan
property in your entityManager
/ sessionFactory
configuration.
您应该packagesToScan
在entityManager
/sessionFactory
配置中设置属性。
for example:
例如:
packagesToScan="com.manager"
packagesToScan="com.manager"
Alternatively, if you are using Spring Boot, you can use @EntityScan
annotation:
或者,如果您使用的是 Spring Boot,则可以使用@EntityScan
注释: