hibernate.cfg.xml
------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
<property name="hibernate.connection.username">hr</property>
<property name="hibernate.connection.password">hr</property>
<property name="hibernate.hbm2ddl.auto" >update</property>
<property name="hibernate.show_sql">true</property>
<mapping class="org.jl.vo.StudentTo"/>
<mapping class="org.jl.vo.HobbyTo"/>
</session-factory>
</hibernate-configuration>
2)
HobbyTo.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.jl.vo;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
/**
*
* @author Rishitha
*/
@Entity
@Table(name="jlhobbies")
public class HobbyTo {
@Id
private int hobbyId;
private String hobbyName;
private String hobbyCategory;
@ManyToOne
@JoinColumn(name = "sid")
private StudentTo student;
public HobbyTo(){}
public int getHobbyId() {
return hobbyId;
}
public void setHobbyId(int hobbyId) {
this.hobbyId = hobbyId;
}
public String getHobbyName() {
return hobbyName;
}
public void setHobbyName(String hobbyName) {
this.hobbyName = hobbyName;
}
public String getHobbyCategory() {
return hobbyCategory;
}
public void setHobbyCategory(String hobbyCategory) {
this.hobbyCategory = hobbyCategory;
}
public StudentTo getStudent() {
return student;
}
public void setStudent(StudentTo student) {
this.student = student;
}
}
3)
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.jl.vo;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
/**
*
* @author Rishitha
*/
@Entity
@Table(name="jlstudents")
public class StudentTo {
@Id
private int sid;
@Column(name="jlsfname")
private String sfname;
@Column(name="jlslname")
private String slname;
@Temporal(TemporalType.DATE)
private Date sbdate;
@OneToMany(mappedBy="student",cascade = {javax.persistence.CascadeType.ALL}, orphanRemoval=true)
private Set<HobbyTo> hobbies = new HashSet();
public StudentTo(){
}
public Date getSbdate() {
return sbdate;
}
public void setSbdate(Date sbdate) {
this.sbdate = sbdate;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSfname() {
return sfname;
}
public void setSfname(String sfname) {
this.sfname = sfname;
}
public String getSlname() {
return slname;
}
public void setSlname(String slname) {
this.slname = slname;
}
public Set<HobbyTo> getHobbies() {
return hobbies;
}
public void setHobbies(Set<HobbyTo> hobbies) {
this.hobbies = hobbies;
}
}
4)
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//drop table jlstudents;
//drop table jlcourses;
//
//select * from jlstudents;
//select * from jlhobbies;
//
//drop table jlhobbies;
//drop table jlstudents;
package hibex;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.jl.vo.HobbyTo;
import org.jl.vo.StudentTo;
/**
*
* @author Rishitha
*/
public class RetrievingAllStudentRecordsUsingHQLListAndIterate10 {
public static void main(String args[]){
SessionFactory sf = new Configuration().configure().buildSessionFactory();
//Session is very important
//and using session we do every thing in hibernate.
//Eg We can save,update,delete,load,get etc.........
Session ses = sf.openSession();
//Now let us see how to add the hobbies to student1
Transaction tx = null;
try{
tx = ses.beginTransaction();
String allStudentHql = "FROM StudentTo ";
Iterator it = null;
it = ses.createQuery(allStudentHql).iterate();
System.out.println("After iterate call ");
while(it.hasNext()){
StudentTo st = (StudentTo)it.next();
System.out.println("StudentFirstName: "+st.getSfname()+" StudentLatName: "+st.getSlname());
}
System.out.println("Observe the output carefully with query.iterate "
+ " One query to retrieve all student ids "
+ " and n queries to retrieve the details "
+ "Here n is the no of students ");
System.out.println("If there are 3 students 1 query to retrieve just primary keys"
+ "and 3 queries to retrieve student details ");
//Output
// Hibernate: select studentto0_.sid as col_0_0_ from jlstudents studentto0_
//After Iterate Call
//Hibernate: select studentto0_.sid as col_0_0_ from jlstudents studentto0_
//Hibernate: select studentto0_.sid as sid0_0_, studentto0_.sbdate as sbdate0_0_, studentto0_.jlsfname as jlsfname0_0_, studentto0_.jlslname as jlslname0_0_ from jlstudents studentto0_ where studentto0_.sid=?
//StudentFirstName: Anilkumar StudentLatName: Chintha
//Hibernate: select studentto0_.sid as sid0_0_, studentto0_.sbdate as sbdate0_0_, studentto0_.jlsfname as jlsfname0_0_, studentto0_.jlslname as jlslname0_0_ from jlstudents studentto0_ where studentto0_.sid=?
//StudentFirstName: Rishitha StudentLatName: Chintha
//Hibernate: select studentto0_.sid as sid0_0_, studentto0_.sbdate as sbdate0_0_, studentto0_.jlsfname as jlsfname0_0_, studentto0_.jlslname as jlslname0_0_ from jlstudents studentto0_ where studentto0_.sid=?
//StudentFirstName: Sunilkumar StudentLatName: Chintha
//Hibernate: select studentto0_.sid as sid0_0_, studentto0_.sbdate as sbdate0_0_, studentto0_.jlsfname as jlsfname0_0_, studentto0_.jlslname as jlslname0_0_ from jlstudents studentto0_ where studentto0_.sid=?
//StudentFirstName: FourthStudentFname StudentLatName: FourthStudentLname
//Observe the output carefully with query.iterate One query to retrieve all student ids and n queries to retrieve the details Here n is the no of students
//If there are 3 students 1 query to retrieve just primary keysand 3 queries to retrieve student details
System.out.println("************* Using query.list method ");
List stList = ses.createQuery(allStudentHql).list();
Iterator its = stList.iterator();
while(its.hasNext()){
StudentTo st = (StudentTo)its.next();
System.out.println("StudentFirstName: "+st.getSfname()+" StudentLatName: "+st.getSlname());
}
System.out.println("Observe the output carefully Only One select query is executed"
+ "to get all the student details ");
// ************* Using query.list method
// Hibernate: select studentto0_.sid as sid0_, studentto0_.sbdate as sbdate0_, studentto0_.jlsfname as jlsfname0_, studentto0_.jlslname as jlslname0_ from jlstudents studentto0_
// StudentFirstName: Anilkumar StudentLatName: Chintha
// StudentFirstName: Rishitha StudentLatName: Chintha
// StudentFirstName: Sunilkumar StudentLatName: Chintha
// StudentFirstName: FourthStudentFname StudentLatName: FourthStudentLname
tx.commit();
}catch(Exception e ){
tx.rollback();
e.printStackTrace();
}
}
}
------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
<property name="hibernate.connection.username">hr</property>
<property name="hibernate.connection.password">hr</property>
<property name="hibernate.hbm2ddl.auto" >update</property>
<property name="hibernate.show_sql">true</property>
<mapping class="org.jl.vo.StudentTo"/>
<mapping class="org.jl.vo.HobbyTo"/>
</session-factory>
</hibernate-configuration>
2)
HobbyTo.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.jl.vo;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
/**
*
* @author Rishitha
*/
@Entity
@Table(name="jlhobbies")
public class HobbyTo {
@Id
private int hobbyId;
private String hobbyName;
private String hobbyCategory;
@ManyToOne
@JoinColumn(name = "sid")
private StudentTo student;
public HobbyTo(){}
public int getHobbyId() {
return hobbyId;
}
public void setHobbyId(int hobbyId) {
this.hobbyId = hobbyId;
}
public String getHobbyName() {
return hobbyName;
}
public void setHobbyName(String hobbyName) {
this.hobbyName = hobbyName;
}
public String getHobbyCategory() {
return hobbyCategory;
}
public void setHobbyCategory(String hobbyCategory) {
this.hobbyCategory = hobbyCategory;
}
public StudentTo getStudent() {
return student;
}
public void setStudent(StudentTo student) {
this.student = student;
}
}
3)
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.jl.vo;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
/**
*
* @author Rishitha
*/
@Entity
@Table(name="jlstudents")
public class StudentTo {
@Id
private int sid;
@Column(name="jlsfname")
private String sfname;
@Column(name="jlslname")
private String slname;
@Temporal(TemporalType.DATE)
private Date sbdate;
@OneToMany(mappedBy="student",cascade = {javax.persistence.CascadeType.ALL}, orphanRemoval=true)
private Set<HobbyTo> hobbies = new HashSet();
public StudentTo(){
}
public Date getSbdate() {
return sbdate;
}
public void setSbdate(Date sbdate) {
this.sbdate = sbdate;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSfname() {
return sfname;
}
public void setSfname(String sfname) {
this.sfname = sfname;
}
public String getSlname() {
return slname;
}
public void setSlname(String slname) {
this.slname = slname;
}
public Set<HobbyTo> getHobbies() {
return hobbies;
}
public void setHobbies(Set<HobbyTo> hobbies) {
this.hobbies = hobbies;
}
}
4)
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//drop table jlstudents;
//drop table jlcourses;
//
//select * from jlstudents;
//select * from jlhobbies;
//
//drop table jlhobbies;
//drop table jlstudents;
package hibex;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.jl.vo.HobbyTo;
import org.jl.vo.StudentTo;
/**
*
* @author Rishitha
*/
public class RetrievingAllStudentRecordsUsingHQLListAndIterate10 {
public static void main(String args[]){
SessionFactory sf = new Configuration().configure().buildSessionFactory();
//Session is very important
//and using session we do every thing in hibernate.
//Eg We can save,update,delete,load,get etc.........
Session ses = sf.openSession();
//Now let us see how to add the hobbies to student1
Transaction tx = null;
try{
tx = ses.beginTransaction();
String allStudentHql = "FROM StudentTo ";
Iterator it = null;
it = ses.createQuery(allStudentHql).iterate();
System.out.println("After iterate call ");
while(it.hasNext()){
StudentTo st = (StudentTo)it.next();
System.out.println("StudentFirstName: "+st.getSfname()+" StudentLatName: "+st.getSlname());
}
System.out.println("Observe the output carefully with query.iterate "
+ " One query to retrieve all student ids "
+ " and n queries to retrieve the details "
+ "Here n is the no of students ");
System.out.println("If there are 3 students 1 query to retrieve just primary keys"
+ "and 3 queries to retrieve student details ");
//Output
// Hibernate: select studentto0_.sid as col_0_0_ from jlstudents studentto0_
//After Iterate Call
//Hibernate: select studentto0_.sid as col_0_0_ from jlstudents studentto0_
//Hibernate: select studentto0_.sid as sid0_0_, studentto0_.sbdate as sbdate0_0_, studentto0_.jlsfname as jlsfname0_0_, studentto0_.jlslname as jlslname0_0_ from jlstudents studentto0_ where studentto0_.sid=?
//StudentFirstName: Anilkumar StudentLatName: Chintha
//Hibernate: select studentto0_.sid as sid0_0_, studentto0_.sbdate as sbdate0_0_, studentto0_.jlsfname as jlsfname0_0_, studentto0_.jlslname as jlslname0_0_ from jlstudents studentto0_ where studentto0_.sid=?
//StudentFirstName: Rishitha StudentLatName: Chintha
//Hibernate: select studentto0_.sid as sid0_0_, studentto0_.sbdate as sbdate0_0_, studentto0_.jlsfname as jlsfname0_0_, studentto0_.jlslname as jlslname0_0_ from jlstudents studentto0_ where studentto0_.sid=?
//StudentFirstName: Sunilkumar StudentLatName: Chintha
//Hibernate: select studentto0_.sid as sid0_0_, studentto0_.sbdate as sbdate0_0_, studentto0_.jlsfname as jlsfname0_0_, studentto0_.jlslname as jlslname0_0_ from jlstudents studentto0_ where studentto0_.sid=?
//StudentFirstName: FourthStudentFname StudentLatName: FourthStudentLname
//Observe the output carefully with query.iterate One query to retrieve all student ids and n queries to retrieve the details Here n is the no of students
//If there are 3 students 1 query to retrieve just primary keysand 3 queries to retrieve student details
System.out.println("************* Using query.list method ");
List stList = ses.createQuery(allStudentHql).list();
Iterator its = stList.iterator();
while(its.hasNext()){
StudentTo st = (StudentTo)its.next();
System.out.println("StudentFirstName: "+st.getSfname()+" StudentLatName: "+st.getSlname());
}
System.out.println("Observe the output carefully Only One select query is executed"
+ "to get all the student details ");
// ************* Using query.list method
// Hibernate: select studentto0_.sid as sid0_, studentto0_.sbdate as sbdate0_, studentto0_.jlsfname as jlsfname0_, studentto0_.jlslname as jlslname0_ from jlstudents studentto0_
// StudentFirstName: Anilkumar StudentLatName: Chintha
// StudentFirstName: Rishitha StudentLatName: Chintha
// StudentFirstName: Sunilkumar StudentLatName: Chintha
// StudentFirstName: FourthStudentFname StudentLatName: FourthStudentLname
tx.commit();
}catch(Exception e ){
tx.rollback();
e.printStackTrace();
}
}
}
No comments:
Post a Comment