ArunM
ArunM

Reputation: 2314

spring-data-jpa :- Not an managed type Error

Spring Data JPA is not recognising my entity when running tests. Please find code below. We do not use Spring boot.

JPA Config File(JPAConfigration_Test.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:tx="http://www.springframework.org/schema/tx"
       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/tx 
                           http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.springframework.org/schema/data/jpa
                           http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
    <!--Configing Core JPA Repositories-->
    <jpa:repositories base-package="org.test.repository"  entity-manager-factory-ref="entityManagerFactory_core" transaction-manager-ref="transactionManager"/>
    <!--    Configring Core Data Resource-->
    <bean id="DS_TEST" class="org.springframework.jndi.JndiObjectFactoryBean" scope="singleton">
        <property name="jndiName" value="java:comp/env/jdbc/ladvice_service" />
        <property name="resourceRef" value="true" />        
    </bean>
    <!--Configring Core System Entity Manager-->
    <bean id="entityManagerFactory_core" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceXmlLocation" value="classpath:config/persistence.xml" />
        <property name="persistenceUnitName" value="LA_Persistence" />        
        <property name="dataSource" ref="DS_TEST" />
        <property name="packagesToScan" value="org.test.model" />
        <property name="jpaProperties">
            <props>

                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <!-- always puts logging out to the console...we want it in the log file -->
                <prop key="hibernate.connection.show_sql">false</prop>
                <prop key="hibernate.hbm2ddl.auto">none</prop>
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
                <prop key="hibernate.cache.use_second_level_cache">false</prop>
                <prop key="hibernate.cache.use_structured_entries">true</prop>
                <prop key="hibernate.cache.use_query_cache">false</prop>
                <prop key="hibernate.generate_statistics">true</prop>
                <prop key="hibernate.default_batch_fetch_size">500</prop>
                <prop key="hibernate.max_fetch_depth">5</prop>
                <prop key="hibernate.jdbc.batch_size">1000</prop>
                <prop key="hibernate.use_outer_join">true</prop>
            </props>
        </property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="false" />
                <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
            </bean>
        </property>
    </bean>
    <!--Confirgring Core Transaction Manager-->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory_core" />
    </bean>
</beans>

Repository

public interface DemoRepository extends JpaRepository<Demo, Integer> {}

Entity

@Entity
@Table(schema="TESTUSER1")
public class Demo {

    @Id
    private Integer id;

    @Column
    private String description;
}

Persistence

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="LA_Persistence">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
            <property name="hibernate.show_sql" value="false"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
            <property name="hibernate.cache.use_second_level_cache" value="false"/>
            <property name="hibernate.cache.use_query_cache" value="false"/>
        </properties>
    </persistence-unit>
</persistence>

Test

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring/JPAConfigration_Test.xml"})
public class LegalRequestRepositoryTest{

    @BeforeClass
    public static void  initiateJNDI() throws IllegalStateException, NamingException{
        SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
        BasicDataSource basicDataSourceLAdvice = new BasicDataSource();
        basicDataSourceLAdvice.setDriverClassName("oracle.jdbc.OracleDriver");
        basicDataSourceLAdvice.setUsername("TESTUSER1");
        basicDataSourceLAdvice.setPassword("TESTUSER1");
        basicDataSourceLAdvice.setUrl("jdbc:oracle:thin:@localhost:1521:XE");
        builder.bind( "java:comp/env/jdbc/ladvice_service" , basicDataSourceLAdvice );
        builder.activate();
    }

    @Test
    public void testDEMO(){

    }
}

Fails to Load Application Context. Error I get is

    at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:86) ~[spring-test-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    ... 26 common frames omitted
Caused by: java.lang.IllegalArgumentException: Not an managed type: class org.test.model.Demo
    at org.hibernate.ejb.metamodel.MetamodelImpl.managedType(MetamodelImpl.java:200) ~[hibernate-entitymanager-4.2.7.Final.jar:4.2.7.Final]
    at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:68) ~[spring-data-jpa-1.7.3.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getMetadata(JpaEntityInformationSupport.java:67) ~[spring-data-jpa-1.7.3.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:145) ~[spring-data-jpa-1.7.3.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:89) ~[spring-data-jpa-1.7.3.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:69) ~[spring-data-jpa-1.7.3.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:173) ~[spring-data-commons-1.9.3.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:239) ~[spring-data-commons-1.9.3.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:225) ~[spring-data-commons-1.9.3.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92) ~[spring-data-jpa-1.7.3.RELEASE.jar:na]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633) ~[spri

I am able to connect to the DB and the table does exist. Why does this happen ?

UPDATE 1

If the entity is added as <class> in Persistence.xml this seems to work. The problem seems to be with packagesToScan in LocalContainerEntityManagerFactoryBean

Upvotes: 2

Views: 3241

Answers (1)

Oliver Drotbohm
Oliver Drotbohm

Reputation: 83051

Scanning for entities is only used if no persistence unit is defined on the LocalContainerEntityManagerFactory. You define a persistence unit which doesn't contain any persistent classes so that Hibernate ends up knowing about no classes whatsoever.

You're entire persistence.xml seems to be redundant as you just duplicate configuration that's already available on the LCEMFB. I suggest to remove it and let Spring scan for entities.

Upvotes: 3

Related Questions