【问题标题】:JPA - Entities not persistedJPA - 实体未持久化
【发布时间】:2012-02-02 11:49:42
【问题描述】:

这是我的 persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence 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_1_0.xsd" version="1.0">
    <persistence-unit name="miniDS" transaction-type="JTA">
    <jta-data-source>java:/miniDS</jta-data-source>

    <class>com.company.model.Ordre</class>

    <properties>
        <!-- Options Hibernate -->
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="true" />
        <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="hibernate.default_schema" value="mini" />
                    <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup" />
    </properties>
</persistence-unit>

我的代码:

// Create order
Ordre o = new Ordre();
o.setDate(req.getParameter("date"));
o.setMotif(req.getParameter("motif"));

log.info("Ordre: " + o.getDate() + " " + o.getMotif());

OrdreService os = new OrdreService();
os.persist(o);//This method is NOT even called !

// Process application flow here...

OrdreService.java :
public class OrdreService {
private OrdreDAO dao;

public OrdreService() {
    dao = new OrdreDAO();
}

public void persist(Ordre o) {
    System.out.println("Service persist");
    dao.persist(o);
}
     //...
 }

OrdreDAO.java :
public class OrdreDAO {
private EntityManagerFactory emf;
private EntityManager em;

public OrdreDAO() {
    emf = Persistence.createEntityManagerFactory("miniDS");
    em = emf.createEntityManager();
}

public void persist(Ordre o) {
    System.out.println("DAO persist");
    EntityTransaction et = null;

    try {
        et = em.getTransaction();

        et.begin();
        em.persist(o);

        System.out.println("commit ?");

        if (et != null) {
            if (et.isActive()) {
                et.commit();
            }
        }
    } catch (Throwable t) {
        t.printStackTrace();
        if (et != null) {
            if (et.isActive()) {
                et.rollback();
            }
        }
    }
}

 //...
}

OrdreService.persist 也永远不会被调用:\ OrdreDAO.persist。

JBoss 怎么了?

JBoss 5.1.0.GA
Postgresql 8.3
JPA 1

【问题讨论】:

  • 日志消息会很好。大多数情况下,您可以通过仔细阅读来判断缺少的内容
  • 我已经添加了这个:&lt;property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup" /&gt; 但我的实体似乎没有被持久化。道甚至不叫
  • 尝试将transaction-type="JTA" 属性添加到persistence-unit 标记(如果您在JSE(而不是JEE)环境中,则默认为RESOURCE_LOCAL,这是不可取的)。

标签: java hibernate jpa jboss


【解决方案1】:

当您使用“&lt;jta-data-source&gt;”时,在 persistence.xml 文件中将您的事务类型设置为 JTA:

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

【讨论】:

  • 我添加了事务类型并指示事务管理器查找,但我的实体没有持久化。我已经用代码更新了我的帖子。
  • 您必须提供一些代码和/或更多详细信息。很可能您的交易未提交。您是否尝试在 EJB 中持久化实体?你是怎么做到的?
  • 是的,但这还不够,您必须在事务上下文中进行,所以要么启动用户事务,要么在事务 EJB 中进行
  • 它被称为 t=em.getTransaction(); t.开始()。还不够吗?
  • 最后的 t.commit() 怎么样?
猜你喜欢
  • 2017-06-22
  • 1970-01-01
  • 1970-01-01
  • 2011-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-06
  • 1970-01-01
相关资源
最近更新 更多