创建EntityManagerFactory时出现异常 - 在SerialContext中查找'persistence / myPU'失败 [英] Exception while creating EntityManagerFactory - Lookup failed for 'persistence/myPU' in SerialContext

查看:123
本文介绍了创建EntityManagerFactory时出现异常 - 在SerialContext中查找'persistence / myPU'失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Glassfish应用服务器,并尝试将我的spring-hibernate Web应用程序连接到我的db,我有以下配置:

在Glassfish中,



我还添加了jndi名称的JDBC资源(在glassfish中): jdbc / myResource

STRONG>。此资源位于我已创建和测试的连接池中(在第1步中)。



我的EntityManaget用@PersistenceContext注释:

  @PersistenceContext(unitName =myPU)
受保护的EntityManager entityManager;

在\src\main\resources\META-INF\下创建了我的持久性.xml文件:

 < persistence-unit name =myPU> 
< provider> org.hibernate.ejb.HibernatePersistence< / provider>
< jta-data-source> jdbc / myResource< / jta-data-source>
<属性>
<! - Glassfish事务管理器查找 - >
< property name =hibernate.transaction.manager_lookup_class
value =org.hibernate.transaction.SunONETransactionManagerLookup/>
< property name =hibernate.dialectvalue =org.hibernate.dialect.H2Dialect>< / property>
< property name =hibernate.show_sqlvalue =true/>
< property name =hibernate.default_schemavalue =PUBLIC/>
< property name =hibernate.format_sqlvalue =true/>
<! - 使用当前实体配置验证现有模式 - >
< property name =hibernate.hbm2ddl.autovalue =validate/>
< / properties>
< / persistence-unit>

这就是我的applicationContext的样子:

  bean id =txManager
class =org.springframework.orm.hibernate4.HibernateTransactionManager>
< / bean>

< tx:annotation-driven transaction-manager =txManager/>

< jee:jndi-lookup id =emfjndi-name =persistence / myPU/>

<! - - 为了启用EntityManager注入 - >
< bean
class =org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor>
< property name =persistenceUnits>
< map>
< entry key =myPUvalue =persistence / myPU/>
< / map>
< / property>
< / bean>

在我的web.xml中有:

 <持久性单元-REF> 
< persistence-unit-ref-name> persistence / myPU< / persistence-unit-ref-name>
< persistence-unit-name> myPU< / persistence-unit-name>





没有定义名为'myPU'的bean。请参阅server.log获取更多详细信息。

为什么它将持久性单元看作一个bean?

任何想法我做错了什么?



提前致谢解决方案

输入=JTA到我的persistence.xml。
由于某种原因,我的印象是,如果我在persistence.xml中指定了一个jta-data-source,那么默认的事务类型应该是JTA,也许它不是。我真的不明白我对这个解决方案的异常的关系,但目前我的persistence.xml有以下行:

 < persistence-unit name =myPUtransaction-type =JTA> 

它就像一个魅力一样。


I'm working with Glassfish application server and trying to connect my spring-hibernate web application to my db, I have the following configurations:

In Glassfish I have added a jdbc connection and send a successful ping to the DB.

I also added a JDBC resource (in glassfish) with jndi name: jdbc/myResource. This resource is under the connection pool in which I have created and tested (in step 1).

My EntityManaget is annotated with @PersistenceContext:

@PersistenceContext(unitName = "myPU")
protected EntityManager entityManager;

Under \src\main\resources\META-INF\ I created my persistence.xml file:

<persistence-unit name="myPU">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>jdbc/myResource</jta-data-source>
    <properties>
        <!-- Glassfish transaction manager lookup -->
        <property name="hibernate.transaction.manager_lookup_class"
            value="org.hibernate.transaction.SunONETransactionManagerLookup" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"></property>
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.default_schema" value="PUBLIC" />
        <property name="hibernate.format_sql" value="true" />
        <!-- Validates the existing schema with the current entities configuration -->
        <property name="hibernate.hbm2ddl.auto" value="validate" />
    </properties>
</persistence-unit>

This is how my applicationContext looks like:

bean id="txManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
</bean>

<tx:annotation-driven transaction-manager="txManager" />

<jee:jndi-lookup id="emf" jndi-name="persistence/myPU" />

<!-- In order to enable EntityManager injection -->
<bean
    class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor">
    <property name="persistenceUnits">
        <map>
            <entry key="myPU" value="persistence/myPU" />
        </map>
    </property>
</bean>

In my web.xml I have:

<persistence-unit-ref>
<persistence-unit-ref-name>persistence/myPU</persistence-unit-ref-name>
<persistence-unit-name>myPU</persistence-unit-name>

When I publish I keep getting:

No bean named 'myPU' is defined. Please see server.log for more details.

Why is it looking for the persistence unit as a bean?
Any ideas what am I doing wrong?

Thanks in advance

解决方案

Resolved - I added transaction-type="JTA" to my persistence.xml. For some reason I was under the impression that if I'm specifying a jta-data-source in my persistence.xml the default transaction-type should be "JTA", maybe it isn't. I don't really understand the relation of the exception I had to this solution but at the moment my persistence.xml has the following line:

<persistence-unit name="myPU" transaction-type="JTA">

And it works like a charm.

这篇关于创建EntityManagerFactory时出现异常 - 在SerialContext中查找'persistence / myPU'失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆