public class Person {
String firstName;
String lastName;
Set addresses = new HashSet();
long id;
/**
* @param name
* @param name2
*/
public Person(String name, String name2) {
super();
// TODO Auto-generated constructor stub
firstName = name;
lastName = name2;
}
public void addAddress(EmailAddress address){
this.addresses.add(address);
}
/**
* @return Returns the addresses.
*/
public Set getAddresses() {
return addresses;
}
/**
* @param addresses The addresses to set.
*/
public void setAddresses(Set addresses) {
this.addresses = addresses;
}
/**
* @return Returns the firstName.
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName The firstName to set.
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return Returns the id.
*/
public long getId() {
return id;
}
/**
* @param id The id to set.
*/
public void setId(long id) {
this.id = id;
}
/**
* @return Returns the lastName.
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName The lastName to set.
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
<hibernate-mapping>
<class name="Person" table="people" >
<id name="id" type="long" column="person_id">
<generator class="increment"/>
</id>
<property name="lastName"/>
<property name="firstName"/>
<set name="addresses" table="EMAIL_ADDRESSES" cascade="all" inverse="true" lazy="true">
<key column="person_id"/>
<one-to-many class="EmailAddress" />
</set>
</class>
</hibernate-mapping>
public class EmailAddress {
String userName;
String host;
Person owner;
long id;
/**
* @param host
* @param name
*/
public EmailAddress(String host, String name) {
super();
// TODO Auto-generated constructor stub
this.host = host;
userName = name;
}
/**
* @return Returns the host.
*/
public String getHost() {
return host;
}
/**
* @param host The host to set.
*/
public void setHost(String host) {
this.host = host;
}
/**
* @return Returns the id.
*/
public long getId() {
return id;
}
/**
* @param id The id to set.
*/
public void setId(long id) {
this.id = id;
}
/**
* @return Returns the owner.
*/
public Person getOwner() {
return owner;
}
/**
* @param owner The owner to set.
*/
public void setOwner(Person owner) {
this.owner = owner;
}
/**
* @return Returns the userName.
*/
public String getUserName() {
return userName;
}
/**
* @param userName The userName to set.
*/
public void setUserName(String userName) {
this.userName = userName;
}
}
<hibernate-mapping>
<class name="EmailAddress" table="EMAIL_ADDRESSES" >
<id name="id" type="long" column="id">
<generator class="increment"/>
</id>
<property name="userName" />
<property name="host" />
<many-to-one name="owner" column="person_id" class="Person" >
</many-to-one>
</class>
</hibernate-mapping>
Following is a test file
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Person newPerson = new Person("Foo", "Baar");
EmailAddress bar = new EmailAddress("foo", "foo.aol.com");
EmailAddress cat = new EmailAddress("baar", "
[email protected]");
EmailAddress rat = new EmailAddress("rat", "
[email protected]");
EmailAddress zat = new EmailAddress("zat", "
[email protected]");
bar.setOwner(newPerson);
cat.setOwner(newPerson);
rat.setOwner(newPerson);
zat.setOwner(newPerson);
newPerson.addAddress(cat);
newPerson.addAddress(bar);
newPerson.addAddress(rat);
newPerson.addAddress(zat);
Transaction tx = null;
Session session = null;
try{
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
tx = session.beginTransaction();
session.save(newPerson);
tx.commit();
}
catch (Exception e){
System.out.println(e.getMessage());
}
finally{
session.close();
}
}
}