JPA - 实体未持久化
这是我的 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
Here is my 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>
My code :
// 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 is never called :\ OrdreDAO.persist too.
What's going on with JBoss ?
JBoss 5.1.0.GA
Postgresql 8.3
JPA 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您使用“
”时,请在 persistence.xml 文件中将事务类型设置为 JTA:When you use '
<jta-data-source>
' set your transaction type to JTA in persistence.xml file: