您好,登錄后才能下訂單哦!
今天給大家介紹一下springboot中entityManagerFactory報(bào)錯(cuò)的解決方法。文章的內(nèi)容小編覺得不錯(cuò),現(xiàn)在給大家分享一下,覺得有需要的朋友可以了解一下,希望對(duì)大家有所幫助,下面跟著小編的思路一起來閱讀吧。
1.查看注解引入是否正確,實(shí)體類和jpa的。
2.檢查包的引用是否有沖突
只是簡(jiǎn)單環(huán)境中使用。它使用JPA PersistenceProvider自動(dòng)檢測(cè)機(jī)制( according to JPA's Java SE bootstrapping ),并且大多數(shù)情況下,你只能定義一下persistence unit name
例如:
<beans> <bean id="myEmf" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> <property name="persistenceUnitName" value="myPersistenceUnit"/> </bean> </beans>
這個(gè)選項(xiàng)是當(dāng)你應(yīng)用發(fā)布在javaee5的服務(wù)器中。你可以參閱自己應(yīng)用服務(wù)器文檔,如何發(fā)布一個(gè)自定義的JPA provider到你的應(yīng)用服務(wù)器中。
例:
<beans> <jee:jndi-lookup id="myEmf" jndi-name="persistence/myPersistenceUnit"/> </beans>
當(dāng)javaee服務(wù)器啟動(dòng)時(shí),會(huì)自動(dòng)檢測(cè)persistence units。實(shí)際上,是檢測(cè)應(yīng)用包中的META-INF/persistence.xml 文件和web.xml中的persistence-unit-ref,以及定義的environment naming。我理解就是JNDI的name。
一般應(yīng)用情景是:
在META-INF/persistence.xml中 使用<jta-data-source>java:/ MySqlDS</jta-data-source> 獲取容器發(fā)布的Datesource。
transactions是使用的javaee容器支持的JTA系統(tǒng),例如tomcat中,可以這樣
如果你的項(xiàng)目準(zhǔn)備部署在tomcat上,要支持jta,則需把相關(guān)的包放在tomcat/lib包下
1)jndi配置,可以把jndi的配置放置在 tomcat/conf/Catalina/域名(如localhost)/項(xiàng)目名.xml
文件的Context節(jié)點(diǎn)下,如下:
<Resource name="" auth="Container" type="javax.sql.DataSource" username="" password="" driveClassName="oracle.jdbc.driver.OracleDriver" url="" maxActive="45" maxIdle="25"/>
jndi也可以配置在server.xml,context.xml中
2)jta UserTransaction配置
在server.xml文件GlobalNamingResources節(jié)點(diǎn)下配置如下:
<!-- Resource configuration for UserTransaction use JOTM --> <Resource name="UserTransaction" auth="Container" type="javax.transaction.UserTransaction" factory="org.objectweb.jotm.UserTransactionFactory" jotm.timeout="60"/>
然后在 項(xiàng)目名.xml 文件的context節(jié)點(diǎn)下加:
<ResourceLink name="UserTransaction" global="UserTransaction" type="javax.transaction.UserTransaction"/>
SPRING 僅僅做的是是把EntityManagerFactory通過依賴注入到應(yīng)用的object中。如果要管理事務(wù),則使用JtaTransactionManager。
這個(gè)選項(xiàng)中,spring扮演了容器的角色。完全掌管JPA。
LocalContainerEntityManagerFactoryBean會(huì)根據(jù)persistence.xml創(chuàng)造一個(gè)PersistenceUnitInfo實(shí)現(xiàn)。
<beans> <bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="someDataSource"/> <property name="loadTimeWeaver"> <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/> </property> </bean> </beans>
不是所有的JPA provider都需要load-time weaving。hibernate就不需要。呵呵。 <property name="loadTimeWeaver">這個(gè)就不是必須的了。。
Persistence.xml配置:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"> <persistence-unit name="myUnit" transaction-type="RESOURCE_LOCAL"> <mapping-file>META-INF/orm.xml</mapping-file> <exclude-unlisted-classes/> </persistence-unit> </persistence>
如何處理多個(gè)persistence units。spring提供了PersistenceUnitManager統(tǒng)一管理。
<bean id="pum" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager"> <property name="persistenceXmlLocations"> <list> <value>org/springframework/orm/jpa/domain/persistence-multi.xml</value> <value>classpath:/my/package/**/custom-persistence.xml</value> <value>classpath*:META-INF/persistence.xml</value> </list> </property> <property name="dataSources"> <map> <entry key="localDataSource" value-ref="local-db"/> <entry key="remoteDataSource" value-ref="remote-db"/> </map> </property> <!-- if no datasource is specified, use this one --> <property name="defaultDataSource" ref="remoteDataSource"/> </bean> <bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceUnitManager" ref="pum"/> <property name="persistenceUnitName" value="myCustomUnit"/> </bean>
dataSources中的key是persistence.xml中配置的datasource名字,value-ref是spring管理的數(shù)據(jù)源。
另外:
EntityManagerFactory是線程安全的,但是EntityManager不是。
public class ProductDaoImpl implements ProductDao { private EntityManagerFactory emf; @PersistenceUnit public void setEntityManagerFactory(EntityManagerFactory emf) { this.emf = emf; } public Collection loadProductsByCategory(String category) { EntityManager em = this.emf.createEntityManager(); try { Query query = em.createQuery("from Product as p where p.category = ?1"); query.setParameter(1, category); return query.getResultList(); } finally { if (em != null) { em.close(); } } } }
這樣使用有個(gè)最大問題就是每次都要?jiǎng)?chuàng)建一個(gè)新的entityManager。那么該怎么辦?
你可以通過@PersistenceContext獲取一個(gè)transactional EntityManager("shared EntityManager")。為什么稱它為transactional?因?yàn)樗且粋€(gè)共享的以及線程安全的當(dāng)前的transactional EntityManager的一個(gè)代理。
public class ProductDaoImpl implements ProductDao { @PersistenceContext private EntityManager em; public Collection loadProductsByCategory(String category) { Query query = em.createQuery("from Product as p where p.category = :category"); query.setParameter("category", category); return query.getResultList(); } }
springboot一種全新的編程規(guī)范,其設(shè)計(jì)目的是用來簡(jiǎn)化新Spring應(yīng)用的初始搭建以及開發(fā)過程,SpringBoot也是一個(gè)服務(wù)于框架的框架,服務(wù)范圍是簡(jiǎn)化配置文件。
以上就是springboot中entityManagerFactory報(bào)錯(cuò)的解決方法的全部?jī)?nèi)容了,更多與springboot中entityManagerFactory報(bào)錯(cuò)的解決方法相關(guān)的內(nèi)容可以搜索億速云之前的文章或者瀏覽下面的文章進(jìn)行學(xué)習(xí)哈!相信小編會(huì)給大家增添更多知識(shí),希望大家能夠支持一下億速云!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。