2012-07-04 86 views
1

我試圖設置一個使用JBoss和Hibernate的Web應用程序,但我無法讓SQL數據庫運行。當我堅持一個對象,並立即調用contains()返回true,但是當我在另一個方法中查找這個對象時,我的數據庫只是空的。掛毯和休眠:調用堅持EntityManager不承諾數據庫

我的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; 
} 

當IM看到通過日誌,我注意到這樣的警告,當我的第二個方法試圖訪問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' 

這是正常的嗎?我究竟做錯了什麼?

回答