【问题标题】:Hibernate Database session.save does not add record in databaseHibernate Database session.save 不在数据库中添加记录
【发布时间】:2014-09-24 15:12:40
【问题描述】:

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

【问题讨论】:

  • 好吧。在这种情况下,Hibernate 会抛出异常。什么是堆栈跟踪?
  • 嘿@Desorder,检查这个txt,我认为它有来自Apache tomcat输出dl.dropboxusercontent.com/u/48247518/stuff.txt的信息
  • 我确信这一切都归结为这个问题:org.hibernate.PropertyValueException:非空属性引用了一个空值或瞬态值:roof.Temp.email

标签: hibernate session


【解决方案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 正在抱怨电子邮件字段为空。 您是否正确设置了此字段?

【讨论】:

    猜你喜欢
    • 2013-04-22
    • 1970-01-01
    • 1970-01-01
    • 2012-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-22
    • 1970-01-01
    相关资源
    最近更新 更多