2014-09-23 94 views
-2

在使用Hibernate,JSF進行項目時,我想出了一個問題。從看起來,我的程序在通過此處使用的addTemp方法存儲數據時遇到問題Roofhelper.java文件休眠數據庫session.save不會在數據庫中添加記錄

public class RoofHelper { 

SessionFactory factory = NewHibernateUtil.getSessionFactory(); 

public static void main(String[] args) { 

    RoofHelper RoofM = new RoofHelper(); 

} 

public String emailAvailable(String email) { 
    String result; 
    Transaction tx = null; 
    Session session = null; 
    session = factory.openSession(); 
    try { 
     tx = session.beginTransaction(); 
     Query query = session.createQuery("from User user where user.email= :email").setParameter("email", email); 
     User user = (User) query.uniqueResult(); 
     tx.commit(); 
     if (user == null) { 
      result = "a"; 
     } else { 
      result = "na"; 
     } 

    } catch (HibernateException e) { 
     if (tx != null) { 
      tx.rollback(); 
     } 
     e.printStackTrace(); 
     result = "dbp"; 
    } finally { 
     session.close(); 
    } 
    return result; 
} 

/* Add to database a temp user */ 
public String addTemp(String email, String password, String fname, String lname, int phoneNumber, Boolean buyer, Boolean seller, Boolean renter, Boolean tenant) { 
    String result; 
    Session session = null; 
    session = factory.openSession(); 
    Transaction tx = null; 
    try { 
     tx = session.beginTransaction(); 
     Temp temp = new Temp(fname, lname, email, password, phoneNumber, buyer, seller, renter, tenant); 
     session.persist(temp); 
     tx.commit(); 
     result = "s"; // success 
    } catch (HibernateException e) { 
     if (tx != null) { 
      tx.rollback(); 
      result = "f";//failure 
     } 
     result = "ff"; 
     e.printStackTrace(); 
    } finally { 
     session.close(); 
    } 
    return result; 
} 

}

而emailAvailable方法執行得很好,在addTemp只是套牢中返回的 「FF」 字符串。

+2

好吧。在這種情況下,Hibernate正在拋出一個異常。什麼是堆棧跟蹤? – Desorder 2014-09-23 21:23:07

+0

嘿@Desorder,檢查這個TXT,我認爲它有來自Apache的Tomcat輸出信息https://dl.dropboxusercontent.com/u/48247518/stuff.txt – sobek 2014-09-23 22:07:40

+0

我相信這一切都歸結爲這個問題:組織。 hibernate.PropertyValueException:非空屬性引用一個空值或瞬態值:roof.Temp.email – sobek 2014-09-23 22:26:27

回答

1
org.hibernate.PropertyValueException: not-null property references a null or transient value : roof.Temp.email 
    at org.hibernate.engine.internal.Nullability.checkNullability(Nullability.java:106) 
    at org.hibernate.action.internal.AbstractEntityInsertAction.nullifyTransientReferencesIfNotAlready(AbstractEntityInsertAction.java:132) 
    ... 

你的堆棧跟蹤是說Hibernate抱怨電子郵件字段爲空。 您是否正確設置此字段?

+0

好吧,我在上面。 – sobek 2014-09-23 22:45:51