2

My applicationContext.xmlis:

<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

<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"

<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-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?

MDaniyal
  • 1,097
  • 3
  • 13
  • 29
  • 1
    `@ComponentScan` does not scan for Entities, since components are not entities. Outside of Spring Boot, there should be a parameter to give your EntityManagerFactory for that, afaik: http://stackoverflow.com/questions/14368119/auto-scan-packages-for-jpa-entities-in-spring – Florian Schaetz Aug 28 '16 at 14:24
  • you mean `` tag in persistence unit is necessary? @FlorianSchaetz – MDaniyal Aug 28 '16 at 14:30
  • 1
    No, as @Maciej Marczuk aready pointed out, there's a parameter for which packages to scan. No need to give each class explicitly. Otherwise you could use Spring Boot, which can automatically scan. – Florian Schaetz Aug 28 '16 at 14:44
  • @FlorianSchaetz a bean is alredy defined in `applicationContext` I have edited the question can you check please? Thanks – MDaniyal Aug 28 '16 at 14:53
  • 1
    If I were you, I would get rid of the persistence.xml, use only Spring to configure JPA there and then your property will work. Or even better, use Spring Boot, which makes it much simpler. – Florian Schaetz Aug 28 '16 at 15:03

2 Answers2

11

JPA and Spring are two different things.

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:

  • configure Spring to read persistence.xml and completely rely on it;
  • don't use persistence.xml and use a Spring configuration only - another type of configuration, where you could use a LocalContainerEntityManagerFactoryBean property packagesToScan. See this post, for example: https://stackoverflow.com/a/16088340/2816631.

Check what Spring javadoc says about the 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.

If you want to use persistence.xml, you have several options:

  • 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-scan feature:

Hibernate auto-scan feature:

<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>
Community
  • 1
  • 1
Artem Novikov
  • 4,087
  • 1
  • 27
  • 33
  • Thanks :) What can I use to configure Spring to use `persistence.xml`? – MDaniyal Aug 28 '16 at 15:00
  • I have added this property it is still not working, I am unable to understand what I am missing :( – MDaniyal Aug 28 '16 at 15:24
  • I have edited my question to add `persistence.xml` can you have a look please? – MDaniyal Aug 28 '16 at 15:27
  • @MDaniyal please, provide more of the applicationContext.xml that refers to jpa there, if anything. – Artem Novikov Aug 28 '16 at 15:30
  • this is the context file https://gist.github.com/anonymous/c25677c6bceef68ff181532cbade928f – MDaniyal Aug 28 '16 at 15:34
  • edited the question to include `applicationContext.xml` kindly have a look Thanks for the help :) – MDaniyal Aug 28 '16 at 15:40
  • @MDaniyal thanks. What error do you get? How did you know it doesn't work? – Artem Novikov Aug 28 '16 at 15:56
  • This is the error I am getting, It is unable to find NamedQuery https://gist.github.com/anonymous/222bb480bc61046f8c6c1cd6b4b1402b – MDaniyal Aug 28 '16 at 16:04
  • This doesn't necessarily mean that entities are not found. It can be a completely different error. Can you, for example, save a new Address? If yes, entites are found and the error is in how you define queries. – Artem Novikov Aug 28 '16 at 16:08
  • when I define a class with `class` tag in perrsistenceUnit I can fetch records but on removing that class tag this error occurs – MDaniyal Aug 28 '16 at 16:10
  • on saving entity I am getting this error: `java.lang.IllegalArgumentException: Unknown entity: com.entity.Address` – MDaniyal Aug 28 '16 at 16:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122043/discussion-between-artem-novikov-and-mdaniyal). – Artem Novikov Aug 28 '16 at 16:20
4

You should set packagesToScan property in your entityManager / sessionFactory configuration.

for example:

packagesToScan="com.manager"

Alternatively, if you are using Spring Boot, you can use @EntityScan annotation:

Spring Boot 1.4+

Spring Boot < 1.4

Maciej Marczuk
  • 3,593
  • 29
  • 28