2016-04-26 36 views
2

Spring數據運行測試時,JPA不識別我的實體。請在下面找到代碼。我們不使用Spring引導。spring-data-jpa: - 不是託管類型錯誤

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

如果實體在Persistence.xml這似乎工作添加爲<class>。這個問題似乎是與packagesToScanLocalContainerEntityManagerFactoryBean

回答

2

掃描爲實體僅用於當沒有持久化單元的LocalContainerEntityManagerFactory定義。你定義了一個不包含任何持久化類的持久化單元,這樣Hibernate最終不用瞭解任何類。

您全部persistence.xml似乎是多餘的,因爲您只需複製已在LCEMFB上提供的配置。我建議刪除它並讓Spring掃描實體。

+0

謝謝。這現在正在爲我工​​作。但是我們有一個工作的Web應用程序,它實際上運行着與我在問題中提出的完全相同的配置。錯誤只發生在測試的情況下。你有什麼想法,爲什麼這個工程在web應用程序,但沒有在測試。 – ArunM

+0

我再次檢查了這一點,可以確認是否有與上述問題完全相同的配置的工作應用程序。這看起來很奇怪,但出於好奇,他們想知道他們是如何以不同的方式運行測試和Web應用程序的。 – ArunM

相關問題