當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Data JPA单元测试 Not a managed type
生活随笔
收集整理的這篇文章主要介紹了
Spring Data JPA单元测试 Not a managed type
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
為什么80%的碼農都做不了架構師?>>> ??
編者注
之前在編寫HavaWeb的框架的時候,就碰到這個問題了。但是由于懶,沒有處理。最近拿起框架繼續處理。在單元測試的時候,確實報出該問題。隨即詳細檢查。
問題描述
在一個典型的單元測試用例中,在啟動Tomcat后正式運行,沒有發生任何問題。能夠正常save數據內容,Spring也沒有報錯。但是在單元測試之中卻發生了錯誤java.lang.IllegalArgumentException: Not a managed type: class xxx.entity.Entity
@RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration("/src/main/webapp") @ContextConfiguration(locations = {"classpath:appconfig/mvc/spring/servlet-context.xml" }) public class EntityJPAServiceTest {@AutowiredEntityJPAService entityJPAService;@Testpublic void getInsert() throws Exception {Entity entity= new Entity();entity.setName("entity01");entity.setDescription(entity.getName() + " Decription");entity.setSpaceType(SpaceType.interior);entity.setTitle("Entity01");entityJPAService.create(entity);}}經過確實的查詢和附錄的鏈接提供的信息發現是配置文件的錯誤
<!-- 對JPA聲明以hibernate進行實現 --><!-- hibernate 5.2.x after --><bean id="persistenceProvider" class="org.hibernate.jpa.HibernatePersistenceProvider"/><!-- hibernate 5.1.x before --><!--<bean id="persistenceProvider" class="org.hibernate.ejb.HibernatePersistence"/>--><bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /><bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/><!-- JPA進行實體掃描 --><bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"><!-- 自動掃描Entity實體 --><property name="packagesToScan" value="org.aicfve"/><!-- 指明JPA的persistence的配置文件位置 --><!-- 注意:如果使用在springmvc做配置,則不要引用persistence.xml,否則單元測試發生錯誤 --><property name="persistenceXmlLocation" value="classpath:appconfig/orm/jpa/persistence.xml" /><property name="persistenceUnitName" value="persistenceUnit"/><!-- 注入dataSource --><property name="dataSource" ref="dataSource"/><property name="jpaVendorAdapter" ref="jpaVendorAdapter" /><!-- 指定具體實現ORM框架的特定屬性 --><property name="jpaProperties"><props><prop key="hibernate.show_sql">true</prop><prop key="hibernate.hbm2ddl.auto">update</prop><prop key="hibernate.dialect">${jdbc.dialect}</prop></props></property></bean>首先在persistence.xml中添加對應的class內容,能夠正確解決該問題,但是隨即帶來的所有的Entity并不通過掃描,而是要手動輸入,工作量太大。放棄了這種方法。
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL"><!-- hibernate 5.2.x after --><!--<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>--><!-- hibernate 5.1.x before --><!--<provider>org.hibernate.jpa.HibernatePersistence</provider>--><!--<properties>--><!--<property name="hibernate.ejb.cfgfile" value="classpath:appconfig/orm/hibernate/hibernate.cfg.xml" />--><!--</properties>--><class>xxx.entity.Entity</class></persistence-unit>主要的發生問題是如下兩句,這兩句
<property name="persistenceXmlLocation" value="classpath:appconfig/orm/jpa/persistence.xml" /><property name="persistenceUnitName" value="persistenceUnit"/>解決方法
上面兩句話都刪除,或者刪除任意一條,都可以解決這個問題。在正式運行與單元測試,不會發生問題。
附錄
不在JPA 的 persistence.xml 文件中配置Entity class的解決辦法
轉載于:https://my.oschina.net/hava/blog/1543382
總結
以上是生活随笔為你收集整理的Spring Data JPA单元测试 Not a managed type的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Hadoop1.x之hdfs集群搭建
- 下一篇: jQuery Validate验证框架详