Apache Karaf,无法注入实体管理器 [英] Apache Karaf, can't inject entity manager
问题描述
我想从容器中使用实体管理器,但是当我尝试访问它时,我会得到
I want to use entity manager from container, but when I try to access it I get
java.lang.IllegalStateException:需要主动协调
java.lang.IllegalStateException: Need active coordination
persistence.xml
persistence.xml
<persistence-unit name="data-point" transaction-type="JTA">
<jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=dvdrental)</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL82Dialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.archive.autodetection" value="class"/>
</properties>
bluprint.xml
bluprint.xml
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 https://osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"
default-activation="eager"
xmlns:tx="http://aries.apache.org/xmlns/transactions/v1.2.0"
xmlns:jpa="http://aries.apache.org/xmlns/jpa/v2.0.0">
<jpa:enable />
<tx:enable-annotations />
<service id="filesEntityManager" ref="filesEntityManagerImpl" interface="ru.bia.karaf.dao.FilesEntityManager"/>
<bean id="filesEntityManagerImpl" class="ru.bia.karaf.dao.FilesEntityManagerImpl"/>
<bean id="testBean" class="ru.bia.karaf.web.TestBean" init-method="test">
<property name="filesEntityManager" ref="filesEntityManagerImpl"/>
</bean>
TestBean.java
TestBean.java
public class TestBean {
private FilesEntityManagerImpl filesEntityManager;
public void test(){
System.out.println("hey bro from init");
System.out.println("filesEntityManager = " + filesEntityManager);
System.out.println("filesEntityManager.getEm() = " + filesEntityManager.getEm());
}
public FilesEntityManagerImpl getFilesEntityManager() {
return filesEntityManager;
}
public void setFilesEntityManager(FilesEntityManagerImpl filesEntityManager) {
this.filesEntityManager = filesEntityManager;
}
}
FilesEntityManagerImpl.java
FilesEntityManagerImpl.java
@OsgiServiceProvider(classes = {FilesEntityManager.class})
@Transactional
public class FilesEntityManagerImpl implements FilesEntityManager {
@PersistenceContext(unitName="data-point")
EntityManager em;
...
}
推荐答案
注入到FilesEntityManagerImpl中的EntityManager是EntityManager的线程本地代理.它的生命周期必然受到协调.
The EntityManager that is injected into FilesEntityManagerImpl is a thread local proxy of the EntityManager. Its lifecycle is bound to a Coordination.
如果您在协调之外访问em,则会出现此错误.您可以使用@Transactional批注来确保协调处于活动状态.如果你 不需要实际的事务,只需协调,然后使用@Transactional(TxType.SUPPORTS).
If you access em outside of a Coordination you get this error. You can make sure a Coordination is active by using the @Transactional annotations. If you do not need an actual transaction but only the Coordination then use @Transactional(TxType.SUPPORTS).
通常,您也不应在与其一起注入的对象之外访问EntityManager.
You should also generally not access the EntityManager outside of the object that is injected with it.
这篇关于Apache Karaf,无法注入实体管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!