我正在尝试使用JBoss和Hibernate设置Web应用程序,但我无法运行SQL数据库。当我持久化对象并立即调用 contains()时返回true,但当我在另一个方法中查找此对象时,我的数据库只是空的。
我的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>
相关代码:
@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;
}
我尝试在这里添加@CommitAfter,导致同样的错误,以及调用getTransaction.begin()和getTransaction.commit()导致
java.sql.SQLException: You cannot commit during a managed transaction!
我的实体是:
@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;
}
当我浏览日志时,我注意到当我的第二个方法尝试访问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'
这是正常的吗?我做错了什么?
答案 0 :(得分:0)
不,我不认为这是正常的。
我从未与JBoss合作过,所以我只能指出那些做过的人: Transaction Handling in Tapestry5 with Jboss-5.1.0GA/JBoss-7.0.2