数据无法使用Hibernate max查询进行检索

时间:2018-03-12 15:17:05

标签: java hibernate javadb

我正在使用Hibernate 4.2.6和Java DB 这是我的作品 RegisterationHelper.java

    org.hibernate.Transaction tx = session.beginTransaction();
    String uid = std.getUserid();
    System.out.println(uid);
    Query query = session.createQuery("FROM university.Student");
    List<Student> student = query.list();
    for(Iterator it= student.iterator(); it.hasNext();) 
    {
       //some code....
        {
            try 
            {
            tx = session.beginTransaction();
            int rgstnum=0;
            Query q = session.createQuery("Select max(registrationnumber)from Student");
            List currentRegNo = q.list();
            rgstnum=(Integer)currentRegNo.get(0)+1;
            std.setRegistrationnumber(rgstnum);
            sc.setRegistrationnumber(rgstnum);
            Serializable objID=session.save(std);
            session.saveOrUpdate(sc);
            tx.commit();
            }
            catch(Exception e)
            {

            }
            //priniting sc.getRegistrationnumber() showing null
           //priniting sc.getCurseid() has data 

RegistrationForm.java

    @ManagedBean
    @RequestScoped
public class RegistrationForm {

public String submitAction() {
            RegistrationHelper rghp = new RegistrationHelper();
            Student std = new Student();
            std.setFirstname(getFirstName().toString());
            std.setLastname(getLastName().toString());
            std.setGender(getGender().toString());
            std.setDob(getDateofBirth());
            std.setAddress(getAddress().toString());
            std.setPhone(getContactNumber().toString());
            std.setEmail(getEmailID().toString());
            std.setUserid(getUserID().toString());
            std.setPassword(getPassword().toString());
            Studentcourse sc = new Studentcourse();
            sc.setCourseid(getCourse().toString());
            String msg = rghp.insertStudent(std, sc);
            if(msg.equals("error"))
            {
                setUserIdError("User Id already exist");
                setUserID("");
                return "Registration";
            }
            else
            {
                return "Success";
            }
        }`

所以我认为只有查询Query q = session.createQuery("Select max(registrationnumber)from Student");没有检索registrationumber的问题,因为如果删除有关的代码,我可以将数据存储在Student表中StudentCourse和其他事情在服务器日志中获得Info: nested transactions not supported 所以,如果我的查询错误或其他原因,请。

1 个答案:

答案 0 :(得分:1)

我不认为查询中存在问题:消息nested transactions not supported指的是您打开事务(session.beginTransaction())两次,一次开始然后再一次打开。尝试删除第二个。