日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Data JPA单元测试 Not a managed type

發布時間:2025/6/15 javascript 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。