Hibernate Database session.save不会在数据库中添加记录

时间:2014-09-23 21:17:08

标签: hibernate session

在使用Hibernate,JSF进行项目时,我提出了一个问题。从看起来,我的程序通过此Roofhelper.java文件中使用的addTemp方法存储数据存在问题

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”字符串。

1 个答案:

答案 0 :(得分: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)
    ...

你的stacktrace说Hibernate抱怨电子邮件字段为空。 你正确设置这个字段吗?