1

我需要您的帮助才能在 jboss 7 和 Hibernate 4 上使用 Seam3 正确配置我的 Web 应用程序。

当我尝试注入 EntityManager 并使用它时,entitymanager 结果为 NULL,但如果尝试通过 Persistence.createEntityManagerFactory("myPU").createEntityManager() 获取 EntityManager,则结果是正确的。

这是我的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>

http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

<persistence-unit name="accbetaPU" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>accbeta</jta-data-source>
    <properties>               
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />            
        <property name="hibernate.transaction.manager_lookup_class"
            value="org.hibernate.transaction.JBossTransactionManagerLookup" />
        <property name="hibernate.archive.autodetection" value="class" />
    </properties>
</persistence-unit>

这是我的 EntityManagerProducer

@ApplicationScoped
public class EntityManagerProducer{

@PersistenceContext
private EntityManager entityManager;

@Produces
public EntityManager getEntityManager() {
    return entityManager;
}
}

这是我的 /Web-INF/beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
<interceptors>
 <class>org.jboss.seam.faces.context.conversation.ConversationBoundaryInterceptor</class>
<class>org.jboss.seam.transaction.TransactionInterceptor</class>
</interceptors>
</beans>

这是我测试EntityManager的课程...

@ApplicationScoped
@Named("first")
public class first implements Serializable{

private static final long serialVersionUID = -1625566649081574918L;
private String testBenvenuto;

@Inject 
   private EntityManager entityManager;


public first() {
    this.testBenvenuto = "Welcome";
    starting();

}

public void starting(){
    this.testBenvenuto = this.testBenvenuto+" "+entityManager;
    this.testBenvenuto = this.testBenvenuto+" "+Persistence.createEntityManagerFactory("accbetaPU").createEntityManager().toString();

}

public String getTestBenvenuto() {
    return testBenvenuto;
}

public void setTestBenvenuto(String testBenvenuto) {
    this.testBenvenuto = testBenvenuto;
}
}

当尝试在 JBOSS As7 中部署(WAR)时,应用程序的部署没有异常......

几个小时后..

我尝试在 EntityManagerProducer 中使用此代码:

@ExtensionManaged
    @ConversationScoped
    @Produces
    @PersistenceUnit(unitName="accbetaPU")
    EntityManagerFactory emf;

但我在部署期间遇到此错误:

16:14:03,550 INFO [org.jboss.solder.core.CoreExtension] (MSC service thread 1-1) Preventing class org.jboss.seam.international.locale.LocaleConfiguration from being installed as bean due to @Veto annotation
16:14:05,715 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-1) MSC00001: Failed to start service jboss.deployment.unit."accbeta.war".WeldService: org.jboss.msc.service.StartException in service jboss.deployment.unit."accbeta.war".WeldService: org.jboss.weld.exceptions.DefinitionException: WELD-001502 Resource producer field [Resource Producer Field [EntityManagerFactory] with qualifiers [@Any @Default] declared as [[field] @PersistenceUnit @ConversationScoped @Produces startup.first.emf]] must be @Dependent scoped
 at org.jboss.as.weld.services.WeldService.start(WeldService.java:96)
 at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1824) [jboss-msc-1.0.1.GA.jar:1.0.1.GA]
 at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1759) [jboss-msc-1.0.1.GA.jar:1.0.1.GA]
 at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [:1.6.0_25]
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [:1.6.0_25]
 at java.lang.Thread.run(Thread.java:662) [:1.6.0_25]
Caused by: org.jboss.weld.exceptions.DefinitionException: WELD-001502 Resource producer field [Resource Producer Field [EntityManagerFactory] with qualifiers [@Any @Default] declared as [[field] @PersistenceUnit @ConversationScoped @Produces startup.first.emf]] must be @Dependent scoped
 at org.jboss.weld.bean.builtin.ee.EEResourceProducerField.checkEEResource(EEResourceProducerField.java:133)
 at org.jboss.weld.bean.builtin.ee.EEResourceProducerField.initialize(EEResourceProducerField.java:125)
 at org.jboss.weld.bootstrap.AbstractBeanDeployer.deploy(AbstractBeanDeployer.java:119)
 at org.jboss.weld.bootstrap.BeanDeployment.deployBeans(BeanDeployment.java:227)
 at org.jboss.weld.bootstrap.WeldBootstrap.deployBeans(WeldBootstrap.java:378)
 at org.jboss.as.weld.WeldContainer.start(WeldContainer.java:81)
 at org.jboss.as.weld.services.WeldService.start(WeldService.java:89)
 ... 5 more

16:14:05,715 INFO [org.jboss.as.controller] (DeploymentScanner-threads - 2) Service status report
 Services which failed to start:
 service jboss.deployment.unit."accbeta.war".WeldService: org.jboss.msc.service.StartException in service jboss.deployment.unit."accbeta.war".WeldService: org.jboss.weld.exceptions.DefinitionException: WELD-001502 Resource producer field [Resource Producer Field [EntityManagerFactory] with qualifiers [@Any @Default] declared as [[field] @PersistenceUnit @ConversationScoped @Produces startup.first.emf]] must be @Dependent scoped

WELD-001502 资源生产者字段 [Resource Producer Field [EntityManagerFactory] ​​与限定符 [@Any @Default] 声明为 [[field] @PersistenceUnit @ConversationScoped @Produces startup.first.emf]] 必须是 @Dependent 范围

我应该怎么办?少了什么东西?

以下是 war 文件中的库列表:

commons-beanutils-1.8.0.jar
commons-digester-2.1.jar
commons-logging-1.1.1.jar
cssparser-0.9.5.jar
guava-0.9.jar
hibernate-entitymanager-4.0.0.CR1.jar
jboss-servlet-api_3.0_spec-1.0.0.Final.jar
joda-time-1.6.jar
log4j-1.2.16.jar
ocpsoft-pretty-time-1.0.6.jar
picketlink-idm-api-1.5.0.Alpha02.jar
picketlink-idm-common-1.5.0.Alpha02.jar
picketlink-idm-core-1.5.0.Alpha02.jar
picketlink-idm-spi-1.5.0.Alpha02.jar
postgresql-9.0-801.jdbc4.jar
prettyfaces-jsf2-3.2.0.jar
richfaces-components-api-4.2.1.Final.jar
richfaces-components-ui-4.2.1.Final.jar
richfaces-core-api-4.2.1.Final.jar
richfaces-core-impl-4.2.1.Final.jar
sac-1.3.jar
seam-faces-3.1.0.Final.jar
seam-faces-api-3.1.0.Final.jar
seam-international-3.1.0.Final.jar
seam-international-api-3.1.0.Final.jar
seam-persistence-3.1.0.Final.jar
seam-persistence-api-3.1.0.Final.jar
seam-security-3.1.0.Final.jar
seam-security-api-3.1.0.Final.jar
slf4j-api-1.6.1.jar
slf4j-simple-1.6.1.jar
solder-api-3.1.0.Final.jar
solder-impl-3.1.0.Final.jar
solder-logging-3.1.0.Final.jar

Jboss版本是7.0.2

请帮帮我


感谢您的回答..我已尝试删除hibernate-entitymanager-4.0.0.CR1.jarjboss-servlet-api_3.0_spec-1.0.0.Final.jar 没有效果!

我也改变了 persistence.xml

<persistence-unit name="accbetaPU">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:jboss/datasources/AccbetaDS</jta-data-source>
        <properties>
            <!-- Properties for Hibernate (default provider for JBoss AS) -->
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
            <property name="hibernate.show_sql" value="true"/>
            <!-- Only format when you need to debug, because it slows things down -->
            <property name="hibernate.format_sql" value="false"/>
            <property name="hibernate.show_sql" value="false"/>
            <property name="hibernate.transaction.manager_lookup_class"
                value="org.hibernate.transaction.JBossTransactionManagerLookup" />
        </properties>
    </persistence-unit>

当我尝试以这种方式创建实体管理器时......

@ExtensionManaged
    @ConversationScoped
    @Produces
    @PersistenceUnit(unitName="accbetaPU")
    EntityManagerFactory emf;

错误是一样的......

17:11:20,589 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-4) MSC00001: Failed to start service jboss.deployment.unit."accbeta.war".WeldService: org.jboss.msc.service.StartException in service jboss.deployment.unit."accbeta.war".WeldService: org.jboss.weld.exceptions.DefinitionException: WELD-001502 Resource producer field [Resource Producer Field [EntityManagerFactory] with qualifiers [@Any @Default] declared as [[field] @ConversationScoped @PersistenceUnit @Produces startup.first.emf]] must be @Dependent scoped
 at org.jboss.as.weld.services.WeldService.start(WeldService.java:96)
 at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1824) [jboss-msc-1.0.1.GA.jar:1.0.1.GA]
 at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1759) [jboss-msc-1.0.1.GA.jar:1.0.1.GA]
 at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [:1.6.0_25]
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [:1.6.0_25]
 at java.lang.Thread.run(Thread.java:662) [:1.6.0_25]
Caused by: org.jboss.weld.exceptions.DefinitionException: WELD-001502 Resource producer field [Resource Producer Field [EntityManagerFactory] with qualifiers [@Any @Default] declared as [[field] @ConversationScoped @PersistenceUnit @Produces startup.first.emf]] must be @Dependent scoped
 at org.jboss.weld.bean.builtin.ee.EEResourceProducerField.checkEEResource(EEResourceProducerField.java:133)
 at org.jboss.weld.bean.builtin.ee.EEResourceProducerField.initialize(EEResourceProducerField.java:125)
 at org.jboss.weld.bootstrap.AbstractBeanDeployer.deploy(AbstractBeanDeployer.java:119)
 at org.jboss.weld.bootstrap.BeanDeployment.deployBeans(BeanDeployment.java:227)
 at org.jboss.weld.bootstrap.WeldBootstrap.deployBeans(WeldBootstrap.java:378)
 at org.jboss.as.weld.WeldContainer.start(WeldContainer.java:81)
 at org.jboss.as.weld.services.WeldService.start(WeldService.java:89)
 ... 5 more

17:11:20,605 INFO [org.jboss.as.controller] (DeploymentScanner-threads - 2) Service status report
 Services which failed to start:
 service jboss.deployment.unit."accbeta.war".WeldService: org.jboss.msc.service.StartException in service jboss.deployment.unit."accbeta.war".WeldService: org.jboss.weld.exceptions.DefinitionException: WELD-001502 Resource producer field [Resource Producer Field [EntityManagerFactory] with qualifiers [@Any @Default] declared as [[field] @ConversationScoped @PersistenceUnit @Produces startup.first.emf]] must be @Dependent scoped

感谢您的回答..我已尝试删除hibernate-entitymanager-4.0.0.CR1.jarjboss-servlet-api_3.0_spec-1.0.0.Final.jar 没有效果!

我也改变了 persistence.xml

<persistence-unit name="accbetaPU">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:jboss/datasources/AccbetaDS</jta-data-source>
        <properties>
            <!-- Properties for Hibernate (default provider for JBoss AS) -->
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
            <property name="hibernate.show_sql" value="true"/>
            <!-- Only format when you need to debug, because it slows things down -->
            <property name="hibernate.format_sql" value="false"/>
            <property name="hibernate.show_sql" value="false"/>
            <property name="hibernate.transaction.manager_lookup_class"
                value="org.hibernate.transaction.JBossTransactionManagerLookup" />
        </properties>
    </persistence-unit>

当我尝试创建实体管理器时,错误是一样的......

4

2 回答 2

0

我将从删除hibernate-entitymanager-4.0.0.CR1.jar和开始jboss-servlet-api_3.0_spec-1.0.0.Final.jar。不确定这是这里的问题,但肯定不需要这些。

另外,不应该<jta-data-source>accbeta</jta-data-source>是这样的<jta-data-source>java:jboss/datasources/accbeta</jta-data-source>吗?诚然,我在 AS 7 中使用 JPA 还不够,但我知道大多数示例看起来都是这样的 :-)

于 2012-05-18T16:54:38.490 回答
0

我读了你的堆栈跟踪,检查这个小修改是否改变了堆栈跟踪。

在您的 EntityManagerFactory 中删除 @ConversationScope 并添加 @Dependent 注释。

我提到这一点是为了:

Caused by: org.jboss.weld.exceptions.DefinitionException: WELD-001502 Resource producer field [Resource Producer Field [EntityManagerFactory] with qualifiers [@Any @Default] declared as [[field] @PersistenceUnit @ConversationScoped @Produces startup.first.emf]] must be @Dependent scoped
于 2013-02-18T22:51:30.683 回答