我正在尝试使用 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'
这是正常的吗?我究竟做错了什么?