从休眠

时间:2018-09-22 16:13:36

标签: java eclipse hibernate

为什么即使我只运行一个hello程序(在单独的Java项目中),也遇到此错误。 实际上,此错误来自休眠状态,但是任何人都可以向我解释问题的根源,所在位置以及解决方法。

请找到我的休眠代码:

public static void main(String[] args) {

    Student_Info student_Info = new Student_Info();

    student_Info.setName("xyz");
    student_Info.setRollNo(101);
    student_Info.setLastName("kumar");
    student_Info.setRecordUpdatedDate(new Date());
    student_Info.setRecordUpdatedTime(new Date());
    student_Info.setRecordUpdatedOn(new Date());
    student_Info.setIsActive(false);
    student_Info.setDescription("hi this is xyz.");

    Session session = HibernateUtil.getCurrentSession();
    Transaction transaction = null;
    try {
        transaction = session.beginTransaction();
        session.save(student_Info);
        transaction.commit();
    }catch (Exception e) {
        if (transaction != null)
            transaction.rollback();
        e.printStackTrace();
    } finally {
        session.close();                            //close session
        HibernateUtil.getSessionFactory().close();  //close sessionFactory
    }
}

请找到Student_Info的代码

@Entity @Table(name="student")

公共课程Student_Info {

@Id
@GeneratedValue
private int rollNo;

private String name;

@Transient
@Column(name="lastName", nullable=false)
private String lastName;

@Temporal(TemporalType.DATE)
private Date recordUpdatedDate;

@Temporal(TemporalType.TIME)
private Date recordUpdatedTime;

private Date recordUpdatedOn;

private Boolean isActive;

@Lob
private String description;

public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}
public Boolean getIsActive() {
    return isActive;
}
public void setIsActive(Boolean isActive) {
    this.isActive = isActive;
}


public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getRollNo() {
    return rollNo;
}
public void setRollNo(int rollNo) {
    this.rollNo = rollNo;
}

public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}

public Date getRecordUpdatedDate() {
    return recordUpdatedDate;
}
public void setRecordUpdatedDate(Date recordUpdatedDate) {
    this.recordUpdatedDate = recordUpdatedDate;
}
public Date getRecordUpdatedTime() {
    return recordUpdatedTime;
}
public void setRecordUpdatedTime(Date recordUpdatedTime) {
    this.recordUpdatedTime = recordUpdatedTime;
}
public Date getRecordUpdatedOn() {
    return recordUpdatedOn;
}
public void setRecordUpdatedOn(Date recordUpdatedOn) {
    this.recordUpdatedOn = recordUpdatedOn;
}

}

enter image description here

1 个答案:

答案 0 :(得分:0)

我认为您交易有问题。您可以更改交易代码吗?

Transaction transObj = null;
Session sessionObj = null;
try {
    sessionObj = HibernateUtil.buildSessionFactory().openSession();
    transObj = sessionObj.beginTransaction();
    transObj.commit();
} catch (HibernateException exObj) {
    if(transObj!=null){
        transObj.rollback();
    }
    exObj.printStackTrace(); 
} finally {
    sessionObj.close(); 
}

因为您正在使用

Session session = HibernateUtil.getCurrentSession();

,但是您没有任何currentSession。因此,这将在NullPointerException时抛出session.beginTransaction();电话。在我的解决方案会话中,从BuildSessionFactory开始。