File: Main.java
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
public class Main {
public static void main(String[] args) throws Exception {
HibernateUtil hibernateUtil = new HibernateUtil();
hibernateUtil.executeSQLCommand("create table event(event_id int,event_date date, title varchar)");
hibernateUtil.executeSQLCommand("create table person(person_id int,firstname varchar, lastname varchar,age int)");
hibernateUtil.executeSQLCommand("create table PERSON_EVENT(event_id int, person_id int)");
Session session = hibernateUtil.getSession();
Event event1 = new Event();
event1.setTitle("event1");
Event event2 = new Event();
event2.setTitle("event2");
Person person1 = new Person();
person1.setFirstname("person1");
Person person2 = new Person();
person2.setFirstname("person2");
person1.getEvents().add(event1);
person1.getEvents().add(event2);
person2.getEvents().add(event1);
person2.getEvents().add(event2);
session.save(event1);
session.save(event2);
session.save(person1);
session.flush();
session.close();
hibernateUtil.checkData("select * from event");
hibernateUtil.checkData("select * from person");
hibernateUtil.checkData("select * from PERSON_EVENT");
}
}
File: Event.hbm.xml
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
File: Event.java
import java.util.Date;
public class Event {
private Long id;
private String title;
private Date date;
public Event() {}
public Long getId() {
return id;
}
private void setId(Long id) {
this.id = id;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
File: Person.hbm.xml
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
File: Person.java
import java.util.HashSet;
import java.util.Set;
public class Person {
private Long id;
private int age;
private String firstname;
private String lastname;
public Person() {}
private Set events = new HashSet();
public Set getEvents() {
return events;
}
public void setEvents(Set events) {
this.events = events;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
}
File: hibernate.cfg.xml
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
org.hsqldb.jdbcDriver
jdbc:hsqldb:data/tutorial
sa
1
org.hibernate.dialect.HSQLDialect
org.hibernate.context.ManagedSessionContext
false
false
org.hibernate.cache.NoCacheProvider
true
File: HibernateUtil.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
Session session;
Statement st;
Configuration config;
public HibernateUtil() throws Exception{
config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession();
// Load the JDBC driver.
Class.forName("org.hsqldb.jdbcDriver");
System.out.println("Driver Loaded.");
// Establish the connection to the database.
String url = "jdbc:hsqldb:data/tutorial";
Connection conn = DriverManager.getConnection(url, "sa", "");
System.out.println("Got Connection.");
st = conn.createStatement();
}
public Configuration getConfiguration(){
return config;
}
public Session getSession(){
return session;
}
public void executeSQLCommand(String sql) throws Exception {
st.executeUpdate(sql);
}
public void checkData(String sql) throws Exception {
ResultSet rs = st.executeQuery(sql);
ResultSetMetaData metadata = rs.getMetaData();
for (int i = 0; i < metadata.getColumnCount(); i++) {
System.out.print("\t"+ metadata.getColumnLabel(i + 1));
}
System.out.println("\n----------------------------------");
while (rs.next()) {
for (int i = 0; i < metadata.getColumnCount(); i++) {
Object value = rs.getObject(i + 1);
if (value == null) {
System.out.print("\t ");
} else {
System.out.print("\t"+value.toString().trim());
}
}
System.out.println("");
}
}
}