Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Friday, November 23, 2007

java code for One To Many Mapping based on Set

import java.util.*;

import org.hibernate.*;
import org.hibernate.criterion.*;

public class SimpleRetrieveTest {


public static void main(String[] args) {
HibernateUtil.setup("create table EVENTS ( uid int, name VARCHAR, start_Date date, duration int, location_id int);");
HibernateUtil.setup("create table speakers ( uid int, event_id int, firstName VARCHAR, lastName VARCHAR);");

// hibernate code start
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();


Event event = new Event();
event.setName("One-to-many test");
event.setSpeakers(new HashSet());
event.getSpeakers().add(new Speaker("John", "Smith"));
event.getSpeakers().add(new Speaker("Dave", "Smith"));
event.getSpeakers().add(new Speaker("Joan", "Smith"));

session.saveOrUpdate(event);


tx.commit();
HibernateUtil.closeSession();

HibernateUtil.sessionFactory.close();

HibernateUtil.checkData("select * from speakers");
HibernateUtil.checkData("select uid, name from events");
// hibernate code end
}

}



/////////////////////////////////////////////////////////////////////////

"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">







type="date"/>

















/////////////////////////////////////////////////////////////////////////

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;
}

}




/////////////////////////////////////////////////////////////////////////

"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">














/////////////////////////////////////////////////////////////////////////
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;
}

}



/////////////////////////////////////////////////////////////////////////

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">






org.hsqldb.jdbcDriver
jdbc:hsqldb:data/tutorial
sa



1


org.hibernate.dialect.HSQLDialect


true







No comments: