spring-data-jpa: - 不是托管类型错误

时间:2016-04-26 07:39:23

标签: spring jpa spring-data-jpa spring-test

Spring Data JPA在运行测试时无法识别我的实体。请在下面找到代码。我们不使用Spring启动。

JPA配置文件(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>

存储库

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

实体

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

    @Id
    private Integer id;

    @Column
    private String description;
}

持久性

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

测试

@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(){

    }
}

无法加载应用程序上下文。我得到的错误是

    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

我能够连接到数据库,表格确实存在。为什么会这样?

更新1

如果在<class>中将实体添加为Persistence.xml,这似乎有效。问题似乎与packagesToScan

中的LocalContainerEntityManagerFactoryBean有关

1 个答案:

答案 0 :(得分:3)

仅在LocalContainerEntityManagerFactory上定义 no 持久性单元时才使用扫描实体。您定义了一个不包含任何持久化类的持久性单元,以便Hibernate最终知道没有任何类。

您完整persistence.xml似乎是多余的,因为您只需复制LCEMFB上已有的配置。我建议删除它,让Spring扫描实体。