Saturday, 3 March 2018

21.0 Hibernate Example - Read Object

Example 2.a - Read the programmer Object


























-------------------------------------------------------------------------------------------------------------
ReadDemo.java
-------------------------------------------------------------------------------------------------------------
package com.demo.spring.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.demo.spring.entitiy.Programmer;

public class ReadDemo {

public static void main(String[] args) {
SessionFactory factory=new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(Programmer.class)
.buildSessionFactory();

Session session=factory.getCurrentSession();
try{
session.beginTransaction();
Programmer programmer=new Programmer("Amit Kumar","amit.kumar@tiwari.com");
session.save(programmer);
session.getTransaction().commit();
session=factory.getCurrentSession();
session.beginTransaction();
System.out.println("\nGetting student with id : "+ programmer.getId());
Programmer myProgrammer=session.get(Programmer.class, programmer.getId());
System.out.println("Get complete  " + myProgrammer);
System.out.println("Done");
}
finally{
session.close();
}
}

}


-------------------------------------------------------------------------------------------------------------
OUTPUT
-------------------------------------------------------------------------------------------------------------
Mar 04, 2018 1:20:13 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Mar 04, 2018 1:20:13 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Hibernate: insert into programmer (email, Name) values (?, ?)

Getting student with id : 8
Hibernate: select programmer0_.id as id1_0_0_, programmer0_.email as email2_0_0_, programmer0_.Name as Name3_0_0_ from programmer programmer0_ where programmer0_.id=?
Get complete L Programmer [id=8, name=Amit Kumar, email=amit.kumar@tiwari.com]
Done










Example 2.b - Read the programmer Object


-------------------------------------------------------------------------------------------------------------
QueryDemo.java
-------------------------------------------------------------------------------------------------------------
package com.demo.spring.test;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.demo.spring.entitiy.Programmer;

public class QueryDemo {

public static void main(String[] args) {
SessionFactory factory=new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(Programmer.class)
.buildSessionFactory();
Session session=factory.getCurrentSession();
try{
session.beginTransaction();
System.out.println("\nAll Programmers list 1");
List<Programmer> programmer=session.createQuery("from Programmer").list();
displayProgrammer(programmer);
System.out.println("\n\nProgrammers List 2");
programmer=session.createQuery("from Programmer where name like '%Kumar'").list();
displayProgrammer(programmer);
System.out.println("\n\nProgrammers List 3");
programmer=session.createQuery("from Programmer where email like '%tiwari%' and name like '%it%'").list();
displayProgrammer(programmer);
session.getTransaction().commit();
System.out.println("Done!");
}
finally{
session.close();
}
}

private static void displayProgrammer(List<Programmer> programmer) {
for(Programmer tmpProgrammer : programmer){
System.out.println(tmpProgrammer);
}
}

}

-------------------------------------------------------------------------------------------------------------
OUTPUT
-------------------------------------------------------------------------------------------------------------



No comments:

Post a Comment