13

I have created spring data JPA sample project. I used eclips for this. There are two projects, one is domain object project which has only annotated entity classes. Other project is actual spring data jpa project and it has a dependency for the project one. Actually it gets domain project entity classes via maven dependency.

Project 1 : hrm-domain

Entity class :

package com.hrm.ws.data.domain;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;



@Entity(name = "Employee")
@Table(name = "employee")
@Inheritance(strategy = InheritanceType.JOINED)
public class Employee {

    protected long id;

    protected String firstName;

    protected String lastName;

    /**
     * Gets the value of the id property.
     * 
     */
    @Id
    @Column(name = "id", scale = 0)
    @GeneratedValue(strategy = GenerationType.AUTO)
    public long getId() {
        return id;
    }

Project 2 : hrm-ws-service

persistance-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"

       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/data/jpa
                           http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">


        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="${jdbc.driverClassName}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
        </bean>

        <jpa:repositories base-package="com.hrm.ws.data.repository" />


        <bean id="employeeDao" class="com.hrm.ws.data.repository.EmployeeRepositoryImpl"/>      


        <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">

              <property name="entityManagerFactory" ref="entityManagerFactory" />
              <property name="jpaDialect">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
              </property>

        </bean>

        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

              <property name="dataSource" ref="dataSource" />

              <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                  <property name="generateDdl" value="${jdbc.generateDdl}" />
                  <property name="showSql" value="${jdbc.showSql}"/>
                </bean>
              </property>
              <property name="persistenceUnitName" value="jpa.sample" />

        </bean>

</beans>

So the setup for my packages is as follows:

src/main/java - contains the spring repository

src/main/resources - contains application context

src/test/java - contains unit tests

And dependency for the project 1 (hrm-domain)

My problem is when I run this project I am getting error as follows:

... 38 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Not an entity: class com.hrm.ws.data.domain.Employee
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:149)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:102)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1429)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:844)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:786)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:474)
    ... 40 more


**Caused by: java.lang.IllegalArgumentException: Not an entity: class** com.hrm.ws.data.domain.Employee
    at org.hibernate.ejb.metamodel.MetamodelImpl.entity(MetamodelImpl.java:160)
    at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:52)
    at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getMetadata(JpaEntityInformationSupport.java:61)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:145)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:83)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:66)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:146)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:120)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:39)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
    ... 48 more

I am wondering about this issue, because when I put domain object (Employee.java) in my spring project it self rather separate jar it works fine. This issue only happens it gives as a separate jar. If any one has experience about scenario like that please give me a help.

5
  • It is invisible from sample, but do Employee have a constructor? Commented Jun 3, 2012 at 18:25
  • 2
    please, provide the contents of the persistence.xml. The persistence unit name 'jpa.sample', in the bean entityManagerFactory, is it correct? Commented Jun 3, 2012 at 20:45
  • I also have the same problem, could you figure out what the problem was? My project has the annotated classes in a separated maven jpa-facet project (which is referenced from the maven spring based project) Commented Jul 6, 2012 at 22:34
  • Note that entities declared in upstream projects need to be listed in the persistence-unit of the persistence.xml file of the downstream project, in order for the downstream project to use it. Commented Nov 27, 2012 at 16:31
  • May not be related to your problem, but if you have upgraded java to java 8 then spring3 doesn't work. You must upgrade to spring 4 check the given post here Steps to fix the issue given in this post stackoverflow.com/a/30204461/526438 Commented May 13, 2015 at 2:16

3 Answers 3

7

I also had the same problem. Mine was that I didn't supply the packagesToScan property in the entityManagerFactory bean, so Spring couldn't load the Entities. Check out the emf bean declaration in your spring configuration xml. The following is a EMF declaration I hope it could help you to get rid the error:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
    <property name="packagesToScan" value="com.hrm.ws.data.domain" />
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    </property>
</bean>
Sign up to request clarification or add additional context in comments.

1 Comment

I don't have a spring xml configuration file since I'm exclusively using annotations for my bean configuration. Is there a way to do this using annotations with hibernate 5.2.2?
1

You're not scanning the com.hrm.ws.data.domain package for beans annotated with @Entity so the spring container has no knowledge of your entities and therefore producing an exception. Add the following lines to your persistance-context.xml config file:

<context:annotation-config />
<context:component-scan base-package="com.hrm.ws.data.domain" />

You'll also need to change your root xml element as follows so it the context elements are recognized:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/data/jpa
                           http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

And, if you haven't already, add the spring-context dependency to your pom file.

2 Comments

I also have the same problem, what if the entity annotated classes are in another maven project (that of course is referenced from the spring based project)? I make what you said but I keep getting the same error as @Hasitha does... What else could it be?
The maven project with the entities should define a context of its own and scan the packages where the annotated entities exist. The other project (consumer of the one with the entities, which I'm assuming is a main project?) should reference the other context by importing the spring xml file. Here's an example on how to do this: mkyong.com/spring3/spring-3-javaconfig-import-example. That said, it's hard to tell what's actually going on without seeing the code.
0

Use one of the following option :-

  1. context:component-scan base-package="Your Base Package Name" in spring.xml file

  2. set packagesToScan property on the entityManagerFactory

  3. use persistence.xml and list all entities there.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.