Steps :
1. Create a Java Project
2. Write hibernate configuration file and Hiberante util file
3. Write the domain objects
4. Write mapping documents
5. Write Insert and Select client
You can see the project as :
Files which you have to write :
1. hibernate.cfg.xml
2. HibernateUtil.java
3. Student.java
4. student.hbm.xml
5. Nick.java
6. Address.java
7. School.java
8. Bike.java
9. Game.java
10. Account.java
11. H03.java
Codes :
1. hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"file:\\\D:\lib\hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/practice</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">false</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource="com/hibernate/association/student.hbm.xml" />
</session-factory>
</hibernate-configuration> 2. HibernateUtil.java package com.hibernate.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
static SessionFactory factory = null;
static {
try {
Configuration cfg = new Configuration();
cfg = cfg.configure();
factory = cfg.buildSessionFactory();
} catch (Exception e) {
System.out.println("Error in Initializing SessionFactory");
e.printStackTrace();
}
}
public static SessionFactory getSessionFactory() {
return factory;
}
} 3. Student.java package com.hibernate.association;
import java.util.Set;
/**
* Domain class Student
*
* @author bkar
*
*/
public class Student {
private int sid;
private String sname;
// One To One - Primary Key - Bidirectional
private Nick nick;
// One To One - Foreign Key - Bidirectional
private Address address;
// Many To One/One To Many - Bidirectional
private School school;
// One To One - Join Table - Bidirectional
private Bike bike;
// Many To One/One To Many - Bidirectional
private Game game;
// Many To Many - Bidirectional
private Set<Account> accounts;
public Student() {
super();
}
public Student(String sname) {
super();
this.sname = sname;
}
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Nick getNick() {
return nick;
}
public void setNick(Nick nick) {
this.nick = nick;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public School getSchool() {
return school;
}
public void setSchool(School school) {
this.school = school;
}
public Bike getBike() {
return bike;
}
public void setBike(Bike bike) {
this.bike = bike;
}
public Game getGame() {
return game;
}
public void setGame(Game game) {
this.game = game;
}
public Set<Account> getAccounts() {
return accounts;
}
public void setAccounts(Set<Account> accounts) {
this.accounts = accounts;
}
} 4. student.hbm.xml <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping SYSTEM
"file:\\\D:\lib\hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.hibernate.association">
<class name="Student" table="student" lazy="false">
<id name="sid" column="sid" type="int">
<generator class="increment" />
</id>
<property name="sname" column="sname" type="string" />
<!-- One To One - Primary Key - Bidirectional -->
<one-to-one name="nick"/>
<!-- One To One - Foreign Key - Bidirectional -->
<many-to-one name="address" class="Address" column="adid" unique="true" />
<!-- Many To One/One To Many - Bidirectional -->
<many-to-one name="school" class="School" column="schid" />
<!-- Many To Many - Join Table - Bidirectional -->
<!-- Should be above join tags -->
<set name="accounts" table="student_account">
<key column="sid"/>
<many-to-many column="acctId" class="Account"/>
</set>
<!-- One To One - Join Table - Bidirectional -->
<join table="student_bike">
<key column="sid" unique="true" />
<many-to-one name="bike" column="bikeId" unique="true" />
</join>
<!-- Many To One/One To Many - Join Table - Bidirectional -->
<join table="student_game" optional="true">
<key column="sid"/>
<many-to-one name="game" column="gameId"/>
</join>
</class>
<class name="Nick" table="nick" lazy="false">
<id name="nid" column="nid" type="int">
<generator class="increment" />
</id>
<property name="nickName" column="nickName" type="string" />
<!-- One To One - Primary Key - Bidirectional -->
<one-to-one name="student"/>
</class>
<class name="Address" table="address" lazy="false">
<id name="adid" column="adid" type="int">
<generator class="increment" />
</id>
<property name="street" column="street" type="string" />
<property name="city" column="city" type="string" />
<property name="state" column="state" type="string" />
<!-- One To One - Foreign Key - Bidirectional -->
<one-to-one name="student" class="Student" property-ref="address"/>
</class>
<class name="School" table="school" lazy="false">
<id name="schid" column="schid" type="int">
<generator class="increment" />
</id>
<property name="name" column="name" type="string" />
<property name="estbYear" column="estbYear" type="string" />
<!-- Many To One/One To Many - Bidirectional -->
<set name="students">
<key column="schid"/>
<one-to-many class="Student"/>
</set>
</class>
<class name="Bike" table="bike" lazy="false">
<id name="bikeId" column="bikeId" type="int">
<generator class="increment" />
</id>
<property name="bikeNo" column="bikeNo" type="string" />
<property name="bikeBrand" column="bikeBrand" type="string" />
<property name="bikeMfd" column="bikeMfd" type="string"/>
<!-- One To One - Join Table - Bidirectional -->
<join table="student_bike" optional="true" inverse="true">
<key column="bikeId" unique="true"/>
<many-to-one name="student" column="sid" unique="true"/>
</join>
</class>
<class name="Game" table="game" lazy="false">
<id name="gameId" column="gameId" type="int">
<generator class="increment" />
</id>
<property name="gameName" column="gameName" type="string" />
<property name="gamePosition" column="gamePosition" type="string" />
<!-- Many To One/One To Many - Join Table - Bidirectional -->
<set name="students" table="student_game" inverse="true">
<key column="gameId"/>
<many-to-many column="sid" class="Student"/>
</set>
</class>
<class name="Account" table="account" lazy="false">
<id name="acctId" column="acctId" type="int">
<generator class="increment" />
</id>
<property name="acctType" column="acctType" type="string" />
<property name="contacts" column="contacts" type="int" />
<!-- Many To Many - Bidirectional -->
<set name="students" table="student_account">
<key column="acctId"/>
<many-to-many column="sid" class="Student"/>
</set>
</class>
</hibernate-mapping> 5. Nick.java package com.hibernate.association;
/**
* Domain class attached with Student - One to One - Primary Key - Bidirectional
* @author bkar
*
*/
public class Nick {
private int nid;
private String nickName;
// One To One - Primary Key - Bidirectional
private Student student;
public Nick() {
super();
}
public Nick(String nickName) {
super();
this.nickName = nickName;
}
public int getNid() {
return nid;
}
public void setNid(int nid) {
this.nid = nid;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
} 6. Address.java package com.hibernate.association;
/**
* Domain class attached to Student - One to One - Foreign Key - Bidirectional
* @author bkar
*
*/
public class Address {
private int adid;
private String street;
private String city;
private String state;
// One To One - Foreign Key - Bidirectional
private Student student;
public Address() {
super();
}
public Address(String street, String city, String state) {
super();
this.street = street;
this.city = city;
this.state = state;
}
public int getAdid() {
return adid;
}
public void setAdid(int adid) {
this.adid = adid;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
} 7. School.java package com.hibernate.association;
import java.util.Set;
/**
* Doamin class attached to Student - One to Many/Many to One - Bidirectional
* @author bkar
*
*/
public class School {
private int schid;
private String name;
private String estbYear;
private Set<Student> students;
public School() {
super();
}
public School(String name, String estbYear) {
super();
this.name = name;
this.estbYear = estbYear;
}
public int getSchid() {
return schid;
}
public void setSchid(int schid) {
this.schid = schid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEstbYear() {
return estbYear;
}
public void setEstbYear(String estbYear) {
this.estbYear = estbYear;
}
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
} 8. Bike.java package com.hibernate.association;
/**
* Domain class attached to Student - One to One - Join Table - Bidirectional
* @author bkar
*
*/
public class Bike {
private int bikeId;
private String bikeNo;
private String bikeBrand;
private String bikeMfd;
// One To One - Join Table - Bidirectional
private Student student;
public Bike() {
super();
}
public Bike(String bikeNo, String bikeBrand, String bikeMfd) {
super();
this.bikeNo = bikeNo;
this.bikeBrand = bikeBrand;
this.bikeMfd = bikeMfd;
}
public int getBikeId() {
return bikeId;
}
public void setBikeId(int bikeId) {
this.bikeId = bikeId;
}
public String getBikeNo() {
return bikeNo;
}
public void setBikeNo(String bikeNo) {
this.bikeNo = bikeNo;
}
public String getBikeBrand() {
return bikeBrand;
}
public void setBikeBrand(String bikeBrand) {
this.bikeBrand = bikeBrand;
}
public String getBikeMfd() {
return bikeMfd;
}
public void setBikeMfd(String bikeMfd) {
this.bikeMfd = bikeMfd;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
} 9. Game.java package com.hibernate.association;
import java.util.Set;
/**
* Domain class attached to Student - Many to One/One to Many - Join table - Bidirectional
* @author bkar
*
*/
public class Game {
private int gameId;
private String gameName;
private String gamePosition;
// One To Many/Many To One - Join Table - Bidirectional
private Set<Student> students;
public Game() {
super();
}
public Game(String gameName, String gamePosition) {
super();
this.gameName = gameName;
this.gamePosition = gamePosition;
}
public int getGameId() {
return gameId;
}
public void setGameId(int gameId) {
this.gameId = gameId;
}
public String getGameName() {
return gameName;
}
public void setGameName(String gameName) {
this.gameName = gameName;
}
public String getGamePosition() {
return gamePosition;
}
public void setGamePosition(String gamePosition) {
this.gamePosition = gamePosition;
}
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
} 10. Account.java package com.hibernate.association;
import java.util.Set;
/**
* Domain class attached to Student - Many to Many - Join Table - Bidirectional
* @author bkar
*
*/
public class Account {
private int acctId;
private String acctType;
private int contacts;
// Many To Many - Bidirectional
private Set<Student> students;
public Account() {
super();
}
public Account(String acctType, int contacts) {
super();
this.acctType = acctType;
this.contacts = contacts;
}
public int getAcctId() {
return acctId;
}
public void setAcctId(int acctId) {
this.acctId = acctId;
}
public String getAcctType() {
return acctType;
}
public void setAcctType(String acctType) {
this.acctType = acctType;
}
public int getContacts() {
return contacts;
}
public void setContacts(int contacts) {
this.contacts = contacts;
}
@Override
public String toString() {
return "Account("+acctId+")";
}
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
} 11. H03.java package com.client;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.hibernate.association.Account;
import com.hibernate.association.Address;
import com.hibernate.association.Bike;
import com.hibernate.association.Game;
import com.hibernate.association.Nick;
import com.hibernate.association.School;
import com.hibernate.association.Student;
import com.hibernate.util.HibernateUtil;
/**
* Insert and Select Client for
* One To One - Primary Key - Bidirectional
* One To One - Foreign Key - Bidirectional
* One To Many/Many To One - Bidirectional
* One To One - Join Table - Bidirectional
* One To Many/Many To One - Join Table -Bidirectional
* Many To Many - Join Table - Bidirectional
* @author bkar
*/
public class H03 {
public static void main(String[] args) {
SessionFactory sf = null;
Session s = null;
Transaction tx = null;
try {
sf = HibernateUtil.getSessionFactory();
s = sf.openSession();
tx = s.beginTransaction();
// Create Domain Objects
Student stu1 = new Student("Ram");
Student stu2 = new Student("Shyam");
Nick nick1 = new Nick("petV");
Nick nick2 = new Nick("petA");
Address address1 = new Address("street1", "city1", "state1");
Address address2 = new Address("street2", "city2", "state2");
School school1 = new School("ABCD", "2001");
Bike bike1 = new Bike("1111", "Yamaha", "2001");
Bike bike2 = new Bike("2222", "Suzuki", "2002");
Game game1 = new Game("Cricket", "Bowler");
Account account1 = new Account("Facebook", 124);
Account account2 = new Account("Yahoo", 111);
// Assigning the relationship from both sides
stu1.setNick(nick1); // One Student To One Nick - One to One Bidirectional - Primary Key
stu2.setNick(nick2); // One Student To One Nick - One to One Bidirectional - Primary Key
nick1.setStudent(stu1); // One Nick To One Student - One to One Bidirectional - Primary Key
nick2.setStudent(stu2); // One Nick To One Student - One to One Bidirectional - Primary Key
stu1.setAddress(address1); // One Student To One Address - One to One Bidirectional - Foreign Key
stu2.setAddress(address2); // One Student To One Address - One to One Bidirectional - Foreign Key
address1.setStudent(stu1); // One Address To One Student - One to One Bidirectional - Foreign Key
address2.setStudent(stu2); // One Address To One Student - One to One Bidirectional - Foreign Key
stu1.setSchool(school1); // Many Student To One School - Many To One/One To Many Bidirectional
stu2.setSchool(school1); // Many Student To One School - Many To One/One To Many Bidirectional
Set<Student> setofstudents = new HashSet<Student>();
setofstudents.add(stu1);
setofstudents.add(stu2);
school1.setStudents(setofstudents); // One School To Many Student - Many To One/One To Many Bidirectional
stu1.setBike(bike1); // One Student To One Bike - One to One Bidirectional Join Table
stu2.setBike(bike2); // One Student To One Bike - One to One Bidirectional Join Table
bike1.setStudent(stu1); // One Bike To One Student - One to One Bidirectional Join Table
bike2.setStudent(stu2); // One Bike To One Student - One to One Bidirectional Join Table
stu1.setGame(game1); // Many Student To One Game - Many To One/One To Many Bidirectional Join Table
stu2.setGame(game1); // Many Student To One Game - Many To One/One To Many Bidirectional Join Table
game1.setStudents(setofstudents); // One Game To Many Student - Many To One/One To Many Bidirectional Join Table
Set<Account> setofaccounts = new HashSet<Account>();
setofaccounts.add(account1);
setofaccounts.add(account2);
stu1.setAccounts(setofaccounts); // Many Student To Many Account - Many To Many Bidirectional Join Table
stu2.setAccounts(setofaccounts); // Many Student To Many Account - Many To Many Bidirectional Join Table
// account1.setStudents(setofstudents);// Many Account To Many Student - Many To Many Bidirectional Join Table
// account2.setStudents(setofstudents);// Many Account To Many Student - Many To Many Bidirectional Join Table
// Persist the domain objects
s.save(nick1);
s.save(nick2);
s.save(address1);
s.save(address2);
s.save(school1);
s.save(bike1);
s.save(bike2);
s.save(account1);
s.save(account2);
s.save(game1);
s.save(stu1);
s.save(stu2);
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Assigning the relationship in reverse direction only would be enough
Student stu3 = new Student("Gopal");
Student stu4 = new Student("Rahim");
Nick nick3 = new Nick("petB");
Nick nick4 = new Nick("petC");
nick3.setStudent(stu3); // One Nick To One Student - One to One Bidirectional - Primary Key
nick4.setStudent(stu4); // One Nick To One Student - One to One Bidirectional - Primary Key
Address address3 = new Address("street3", "city3", "state3");
Address address4 = new Address("street4", "city4", "state4");
stu3.setAddress(address3); // One Student To One Address - One to One Bidirectional - Foreign Key
stu4.setAddress(address4); // One Student To One Address - One to One Bidirectional - Foreign Key
address3.setStudent(stu3); // One Address To One Student - One to One Bidirectional - Foreign Key
address4.setStudent(stu4); // One Address To One Student - One to One Bidirectional - Foreign Key
School school2 = new School("EFGH","2002");
Set<Student> setofstudents2 = new HashSet<Student>();
setofstudents2.add(stu3);
setofstudents2.add(stu4);
school2.setStudents(setofstudents2); // One School To Many Student - Many To One/One To Many Bidirectional
Bike bike3 = new Bike("3333", "Kawasaki", "2011");
Bike bike4 = new Bike("4444", "Honda", "2012");
stu3.setBike(bike3); // One Student To One Bike - One to One Bidirectional Join Table
stu4.setBike(bike4); // One Student To One Bike - One to One Bidirectional Join Table
bike3.setStudent(stu3); // One Bike To One Student - One to One Bidirectional Join Table
bike4.setStudent(stu4); // One Bike To One Student - One to One Bidirectional Join Table
Game game2 = new Game("Football", "Center");
// game2.setStudents(setofstudents); // Not working - Not able to save the relationship into Join table
stu3.setGame(game2); // Many Student To One Game - Many To One/One To Many Bidirectional Join Table
stu4.setGame(game2); // Many Student To One Game - Many To One/One To Many Bidirectional Join Table
Account account3 = new Account("MySpace", 99);
Account account4 = new Account("Orkut", 234);
Set<Student> setofstu1 = new HashSet<Student>();
setofstu1.add(stu3);
setofstu1.add(stu4);
account3.setStudents(setofstu1); // Many Account To Many Student - Many To Many Bidirectional Join Table
account4.setStudents(setofstu1); // Many Account To Many Student - Many To Many Bidirectional Join Table
s.save(nick3);
s.save(nick4);
s.save(address3);
s.save(address4);
s.save(school2);
s.save(bike3);
s.save(bike4);
s.save(game2);
s.save(account3);
s.save(account4);
s.save(stu3);
s.save(stu4);
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Assigning the relationship in forward direction only would be enough
// Create Domain Objects
Student stu5 = new Student("Nilesh");
Student stu6 = new Student("Neeraj");
Nick nick5 = new Nick("Nilu");
Nick nick6 = new Nick("Neru");
Address address5 = new Address("street5", "city5", "state5");
Address address6 = new Address("street6", "city6", "state6");
School school3 = new School("IJKL", "2003");
Bike bike5 = new Bike("5555", "Yamaha", "2012");
Bike bike6 = new Bike("6666", "Suzuki", "2011");
Game game3 = new Game("Hockey", "Back");
Account account5 = new Account("Google", 12);
Account account6 = new Account("Twitter", 11);
stu5.setNick(nick5); // One Student To One Nick - One to One Bidirectional - Primary Key
stu6.setNick(nick6); // One Student To One Nick - One to One Bidirectional - Primary Key
stu5.setAddress(address5); // One Student To One Address - One to One Bidirectional - Foreign Key
stu6.setAddress(address6); // One Student To One Address - One to One Bidirectional - Foreign Key
stu5.setSchool(school3); // Many Student To One School - Many To One/One To Many Bidirectional
stu6.setSchool(school3); // Many Student To One School - Many To One/One To Many Bidirectional
stu5.setBike(bike5); // One Student To One Bike - One to One Bidirectional Join Table
stu6.setBike(bike6); // One Student To One Bike - One to One Bidirectional Join Table
stu5.setGame(game3); // Many Student To One Game - Many To One/One To Many Bidirectional Join Table
stu6.setGame(game3); // Many Student To One Game - Many To One/One To Many Bidirectional Join Table
Set<Account> setofaccounts3 = new HashSet<Account>();
setofaccounts3.add(account5);
setofaccounts3.add(account6);
stu5.setAccounts(setofaccounts3); // Many Student To Many Account - Many To Many Bidirectional Join Table
stu6.setAccounts(setofaccounts3); // Many Student To Many Account - Many To Many Bidirectional Join Table
// Persist the domain objects
s.save(nick5);
s.save(nick6);
s.save(address5);
s.save(address6);
s.save(school3);
s.save(bike5);
s.save(bike6);
s.save(account5);
s.save(account6);
s.save(game3);
s.save(stu5);
s.save(stu6);
tx.commit();
} catch (Exception e) {
e.printStackTrace();
if (tx != null)
tx.rollback();
}
try {
sf = HibernateUtil.getSessionFactory();
s = sf.openSession();
tx = s.beginTransaction();
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~STUDENT~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
Query query = s.createQuery("From Student");
List stulist = query.list();
for (Object obj : stulist) {
Student stuq = (Student) obj;
System.out.print(
String.format("♣Student{%s|%s} \t♥Nick{%s|%s} \t♥Address{%s|%s|%s|%s] \t♥School{%s|%s|%s} "
+ "\t♠Bike{%s|%s|%s|%s} \t♠Game{%s|%s|%s} \t♠Account[",
stuq.getSid(),
stuq.getSname(),
stuq.getNick().getNid(),
stuq.getNick().getNickName(),
stuq.getAddress().getAdid(),
stuq.getAddress().getStreet(),
stuq.getAddress().getCity(),
stuq.getAddress().getState(),
stuq.getSchool().getSchid(),
stuq.getSchool().getName(),
stuq.getSchool().getEstbYear(),
stuq.getBike().getBikeId(),
stuq.getBike().getBikeNo(),
stuq.getBike().getBikeBrand(),
stuq.getBike().getBikeMfd(),
stuq.getGame().getGameId(),
stuq.getGame().getGameName(),
stuq.getGame().getGamePosition())
);
Set<Account> setOfAccounts = stuq.getAccounts();
for (Account account : setOfAccounts) {
System.out.print(String.format("{%s|%s|%s}",
account.getAcctId(),
account.getAcctType(),
account.getContacts())
);
}
System.out.println("]");
}
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~NICK~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
query = s.createQuery("From Nick");
List nicklist = query.list();
for (Object obj : nicklist) {
Nick nickq = (Nick) obj;
System.out.println(
String.format("♠Nick{%s|%s} \t♠Student{%s|%s}",
nickq.getNid(),
nickq.getNickName(),
nickq.getStudent().getSid(),
nickq.getStudent().getSname())
);
}
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ADDRESS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
query = s.createQuery("From Address");
List addresslist = query.list();
for (Object obj : addresslist) {
Address addressq = (Address) obj;
System.out.println(
String.format("♠Address{%s|%s|%s|%s} \t♠Student{%s|%s}",
addressq.getAdid(),
addressq.getStreet(),
addressq.getCity(),
addressq.getState(),
addressq.getStudent().getSid(),
addressq.getStudent().getSname())
);
}
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~SCHOOL~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
query = s.createQuery("From School");
List schoollist = query.list();
for (Object obj : schoollist) {
School schoolq = (School) obj;
System.out.print(
String.format("♠School{%s|%s|%s} \t♠Student[",
schoolq.getSchid(),
schoolq.getName(),
schoolq.getEstbYear(),
schoolq.getStudents())
);
Set<Student> setOfStudents = schoolq.getStudents();
for (Student st : setOfStudents) {
System.out.print(
String.format("{%s|%s}",
st.getSid(),
st.getSname())
);
}
System.out.println("]");
}
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~BIKE~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
query = s.createQuery("From Bike");
List bikelist = query.list();
for (Object obj : bikelist) {
Bike bikeq = (Bike) obj;
System.out.println(
String.format("♠Bike{%s|%s|%s|%s} \t♠Student{%s|%s}",
bikeq.getBikeId(),
bikeq.getBikeNo(),
bikeq.getBikeBrand(),
bikeq.getBikeMfd(),
bikeq.getStudent().getSid(),
bikeq.getStudent().getSname())
);
}
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~GAME~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
query = s.createQuery("From Game");
List gamelist = query.list();
for (Object obj : gamelist) {
Game gameq = (Game) obj;
System.out.print(
String.format("♠Game{%s|%s|%s} \t♠Student[",
gameq.getGameId(),
gameq.getGameName(),
gameq.getGamePosition(),
gameq.getStudents())
);
Set<Student> setOfStudents = gameq.getStudents();
for (Student st : setOfStudents) {
System.out.print(
String.format("{%s|%s}",
st.getSid(),
st.getSname())
);
}
System.out.println("]");
}
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ACCOUNT~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
query = s.createQuery("From Account");
List accountlist = query.list();
for (Object obj : accountlist) {
Account accountq = (Account) obj;
System.out.print(
String.format("♠Game{%s|%s|%s} \t♠Student[",
accountq.getAcctId(),
accountq.getAcctType(),
accountq.getContacts(),
accountq.getStudents())
);
Set<Student> setOfStudents = accountq.getStudents();
for (Student st : setOfStudents) {
System.out.print(
String.format("{%s|%s}",
st.getSid(),
st.getSname())
);
}
System.out.println("]");
}
tx.commit();
} catch (Exception e) {
e.printStackTrace();
if (tx != null)
tx.rollback();
} finally {
s.close();
sf.close();
}
}
} Queries select * from student;
select * from nick;
select * from address;
select * from school;
select * from student;
select * from bike;
select * from student_bike;
select * from student;
select * from game;
select * from student_game;
select * from student;
select * from account;
select * from student_account;
drop table student_account;
drop table student_game;
drop table student_bike;
drop table account;
drop table game;
drop table bike;
drop table school;
drop table address;
drop table nick;
drop table student; Check DB :
Download the source here : H03







