【问题标题】:persistence.xml transaction-type="JTA"persistence.xml 事务类型="JTA"
【发布时间】:2013-09-20 21:56:22
【问题描述】:

这个perisistence.xml 文件是否正确,尤其是当我transaction-type="JTA" 时?

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="CollDocPU" transaction-type="JTA">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>model.entity.StudentAddSolution</class>
<class>model.entity.Lecturer</class>
<class>model.entity.Solution</class>
<class>model.entity.Student</class>
<class>model.entity.Course</class>
<class>model.entity.File</class>
<class>model.entity.CourseHasLecturer</class>
<class>model.entity.Mail</class>
<class>model.entity.StudentAtCourse</class>
<class>model.entity.Roles</class>
<class>model.entity.Task</class>
<class>model.entity.User</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
  <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:11080/myBase?zeroDateTimeBehavior=convertToNull"/>
  <property name="javax.persistence.jdbc.password" value="pass,"/>
  <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
  <property name="javax.persistence.jdbc.user" value="myBase"/>
  <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
</properties>

【问题讨论】:

    标签: java xml persistence openjpa jta


    【解决方案1】:

    你遇到错误了吗?

    如果您使用的是 JTA,我建议您使用 DataSource 作为连接的提供者。

    如:

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="CollDocPU" transaction-type="JTA">
    <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
    <jta-data-source>datasource</jta-data-source> 
    <class>model.entity.StudentAddSolution</class>
    <class>model.entity.Lecturer</class>
    <class>model.entity.Solution</class>
    <class>model.entity.Student</class>
    <class>model.entity.Course</class>
    <class>model.entity.File</class>
    <class>model.entity.CourseHasLecturer</class>
    <class>model.entity.Mail</class>
    <class>model.entity.StudentAtCourse</class>
    <class>model.entity.Roles</class>
    <class>model.entity.Task</class>
    <class>model.entity.User</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    </persistence-unit>
    
    </persistence>
    

    顺便说一句,您需要关闭所有标签

    问候

    【讨论】:

    • 如何使用 DataSource 作为我的连接提供者?对不起,我是新手,不了解所有内容
    • 按照你的工作方式,我认为你会使用 RESOURCE_LOCA 作为事务类型
    • 但是当我使用“RESOURCE_LOCA”时出现错误:`java.lang.NullPointerException at model.beans.UserFacade.findByLogin(UserFacade.java:34)` 当我使用 JTA 我有错误:Caused by: org.apache.openjpa.lib.jdbc.ReportingSQLException - type not found or user lacks privilege: TEXT {stmnt 1013050998 CREATE TABLE COURSE (id_course SMALLINT NOT NULL, code VARCHAR(255), description TEXT, name VARCHAR(255), realization INTEGER, version SMALLINT, PRIMARY KEY (id_course)) ENGINE = innodb} [code=-5509, state=42509] at org.apache.openjpa.lib.jdbc.LoggingConnectionDecorator.wrap(LoggingConnectionDecora
    • 如何通过@EJB 注解获取EntityManager?如果您在 EJB 中使用 EM,您将使用 PersistenceContext 来检索 entityManager
    • 正如我告诉你的,在这样做之前:EntityManagerFactory emf = Persistence.createEntityManagerFactory("CollDocPU"); EntityManager em = emf.createEntityManager();只需通过 System.out 打印 em 并告诉我你得到了什么
    【解决方案2】:

    我的朋友,也许这是另一个错误,试试这个,按照我告诉你的操作后,只需通过 System.out.println(em.toString()); 打印 EntityManager如果你得到了什么,那是因为没关系,你在其他方法中得到的空指针可能是程序逻辑

    【讨论】:

    • EntityManager 对象引用为空,所以当我有em.createNamedQuery("User.findByLogin").setParameter("login", login); 时,我得到 nullPointerExecption
    • 使用 RESOURCE_LOCAL 并删除 SynchronizeMappings?
    • 试试我告诉你的,同时删除提供者标签,它必须采用环境的默认提供者
    猜你喜欢
    • 2013-06-24
    • 1970-01-01
    • 1970-01-01
    • 2016-09-10
    • 1970-01-01
    • 2015-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多