Friday, 2 August 2013

StatelessSession Interface

StatelessSession Interface

If you want to avoid the high level of hibernate life cycle and the persistance level
then StatelessSession(org.hibernate.StatelessSession) is the solution.It neglects the 1st level and second level of cache.It works similar to org.hibernate.Session .
The link for the Java Doc is this.
 
{
StatelessSession session = sessionFactory.openStatelessSession();
Transaction tx = session.beginTransaction();
   
ScrollableResults students = session.createQuery("from STUDENT").scroll();
    
while ( students.next() ) {
    Student std = (Student) students.get(0);
    session.update(std);
}
   
tx.commit();
session.close();
} 
 


Saturday, 2 March 2013

Update table using HQL

Update table using HQL

This example of code is using the previous hibernate configuration ,orm file and Java class file


  try {
           Session session=HibernateSessionFactory.getSessionFactory().openSession();
           Transaction tx=session.beginTransaction();
            String hql = "UPDATE StudentClass s SET s.studentName= :newName WHERE s.studentId= :studentID";
           // String hql="UPDATE StudentClass  SET studentName= :newName WHERE //studentId= :studentID";
            Query query = session.createQuery(hql);
            query.setParameter("studentID", new Long(1));
            query.setParameter("newName "Nitesh sahay");
            int rowCount = query.executeUpate();
            System.out.println("Rows affected: " + rowCount);
          
        } catch (java.lang.HibernateException ex) {
            ex.printStackTrace();
        }