javahibernatejavadb

Data doesn't retrieving with Hibernate max query


I'm using Hibernate 4.2.6 with Java DB Here's my
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 

and 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";
            }
        }`

So I think there is problem only with query Query q = session.createQuery("Select max(registrationnumber)from Student"); that doesn't retrieving registrationumber cause I'm able to store data in Student table if removing code regarding StudentCourse and other thing is getting Info: nested transactions not supported in sever log.
So please if my query is wrong or something else.


Solution

  • I do not think there are problems in the query: the message nested transactions not supported refers to the fact that you open the transaction (session.beginTransaction()) twice, one at the beginning and then again within the cycle. Try to remove the second.