File: Main.java
import java.util.HashSet;
import org.hibernate.Session;
public class Main {
public static void main(String[] args) throws Exception {
HibernateUtil hibernateUtil = new HibernateUtil();
hibernateUtil
.executeSQLCommand("create table speakers (uid int, firstName varchar,lastName varchar, event_id int);");
hibernateUtil
.executeSQLCommand("create table locations (uid int, name varchar, address varchar, event_id int);");
hibernateUtil
.executeSQLCommand("create table attendees (uid int, firstName varchar, lastName varchar, event_id int);");
hibernateUtil
.executeSQLCommand("create table events (uid int , name varchar, start_date date, duration integer,location_id int);");
Session session = hibernateUtil.getSession();
Event e = new Event();
e.setName("testSave");
Location l = new Location();
l.setName("building 1");
e.setLocation(l);
e.setSpeakers(new HashSet());
e.getSpeakers().add(new Speaker("John", "Doe"));
e.setAttendees(new HashSet());
e.getAttendees().add(new Attendee("John", "Smith"));
//session.save(l);
session.save(e);
// session.flush();
session.close();
hibernateUtil.checkData("select * from events");
hibernateUtil.checkData("select * from speakers");
hibernateUtil.checkData("select * from locations");
hibernateUtil.checkData("select * from attendees");
}
}
File: Attendee.java
public class Attendee {
private Long id;
private String firstName;
private String lastName;
public Attendee() {
}
public Attendee(String firstName, String lastName) {
setFirstName(firstName);
setLastName(lastName);
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
File: Event.hbm.xml
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
type="date"/>
class="Location" cascade="all"/>
File: Event.java
import java.util.Date;
import java.util.Set;
public class Event {
private Long id;
private String name;
private Date startDate;
private int duration;
private Set speakers;
private Set attendees;
private Location location;
public void setId(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
public void setSpeakers(Set speakers) {
this.speakers = speakers;
}
public Set getSpeakers() {
return speakers;
}
public Set getAttendees() {
return attendees;
}
public void setAttendees(Set attendees) {
this.attendees = attendees;
}
}
File: Location.hbm.xml
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
File: Location.java
public class Location {
private Long id;
private String name;
private String address;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
File: Speaker.hbm.xml
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
File: Speaker.java
public class Speaker {
private Long id;
private String firstName;
private String lastName;
public Speaker() {
}
public Speaker(String firstName, String lastName) {
setFirstName(firstName);
setLastName(lastName);
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
File: Attendee.hbm.xml
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
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;
public HibernateUtil() throws Exception{
SessionFactory sessionFactory = new Configuration().configure().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 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("");
}
}
}