简体   繁体   English

Tapestry&Hibernate:在EntityManager上持久调用不会提交到数据库

[英]Tapestry & Hibernate: Calling persist on EntityManager does not commit to database

I'm trying to set up a web application using JBoss and Hibernate, but I can't get the SQL database running. 我正在尝试使用JBoss和Hibernate设置Web应用程序,但无法运行SQL数据库。 When I'm persisting an object and immediately calling contains() returns true, but when I'm looking for this object in another method my database is just empty. 当我保留一个对象并立即调用contains()时,返回true,但是当我在另一个方法中查找该对象时,我的数据库只是空的。

My persistence.xml: 我的persistence.xml:

<?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="facePlace">
<non-jta-data-source>java:jboss/facePlace</non-jta-data-source>
<class>webtech2.faceplace.entities.Person</class>
<properties>
  <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
  <property name="hibernate.hbm2ddl.auto" value="update"/>
  <property name="hibernate.format_sql" value="true"/>
  <property name="hibernate.show_sql" value="true"/>
</properties>

The relevant code: 相关代码:

@Inject
@Persistence
EntityManager em;

public boolean signUp(String name,
      String password,
      String repeatPassword,
      Date birthdate,
      String gender) {
if (!password.equals(repeatPassword)) {
  return false;
}

log.info("person data: " + name + " " + password + " " + repeatPassword + " " + birthdate.toString() + " " + gender);

String saltedPassword = hashText + password;
String hashedPassword = generateHash(saltedPassword);
Person xperson = new Person(name, hashedPassword, birthdate, gender);
em.persist(xperson);
return true;
}

I tried adding @CommitAfter here, resulted in the same error, as well as calling getTransaction.begin() and getTransaction.commit() what lead to 我尝试在此处添加@CommitAfter,导致相同的错误,以及调用getTransaction.begin()和getTransaction.commit()会导致

java.sql.SQLException: You cannot commit during a managed transaction!

My entity is: 我的实体是:

@Entity
public class Person implements Serializable {

private String name;
private Date birthdate;
private long id;
private String password;
private String gender;
private Set<Person> friends;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
public long getId() {
  return id;
}

public void setId(long id) {
  this.id = id;
}

When im seeing through the log I notice this warning that occurs when my second method tries to access the EntityManager: 当我通过日志查看时,我注意到当我的第二种方法尝试访问EntityManager时发生此警告:

HHH000436: Entity manager factory name (facePlace) is already registered.  If entity manager will be clustered or passivated, specify a unique value for property 'hibernate.ejb.entitymanager_factory_name'

Is this normal? 这正常吗? What am I doing wrong? 我究竟做错了什么?

No, I don't think that's normal. 不,我认为这不正常。

I never worked with JBoss though, so I can only point you to people who did: Transaction Handling in Tapestry5 with Jboss-5.1.0GA/JBoss-7.0.2 但是,我从未与JBoss一起工作过,因此,我只能向您指出曾做过的事情: 使用Jboss-5.1.0GA / JBoss-7.0.2在Tapestry5中进行事务处理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM