Here is the Organisation.java:
Code:
/*
* COPYRIGHT
*/
package hibernate;
import java.util.List;
import java.util.ArrayList;
/**
* @author mslv
*
* @hibernate.class
* table="ORGANISATIONS"
*/
public class Organisation {
private Long id;
private Organisation parentId;
private List suborganisations;
private String name;
/**
* @hibernate.id
* generator-class="native"
* column="ORGANISATION_ID"
*/
public Long getId() {
return id;
}
public void setId(Long _id) {
this.id = _id;
}
/**
* @hibernate.many-to-one
* column="PARENT_ID"
* not-null="true"
*/
public Organisation getParentId() {
return parentId;
}
public void setParentId(Organisation parentId) {
this.parentId = parentId;
}
/**
* @hibernate.property
* column="NAME"
* length="1000"
* not-null="true"
* unique="true"
*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getSuborganisations() {
return suborganisations;
}
/**
* @hibernate.bag
* inverse="true"
* lazy="false"
* cascade="all"
* @hibernate.collection-key
* column="PARENT_ID"
* @hibernate.collection-one-to-many
* class="hibernate.Organisation"
*/
public void setSuborganisations(List suborganisations) {
this.suborganisations = suborganisations;
}
public void addSubOrganisation(Organisation org) {
if (suborganisations == null) {
suborganisations = new ArrayList();
}
suborganisations.add(org);
}
public void removeSubOrganisation(Organisation org) {
suborganisations.remove(org);
}
}
The generated .hbm.xml is in the first post.
The main is like this:
Code:
/*
* COPYRIGHT
*/
package hibernate;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.*;
import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import java.sql.SQLException;
public class OrganisationMain {
private SessionFactory _sessions;
public void configure() throws HibernateException {
_sessions = new Configuration()
.addClass(Organisation.class)
.buildSessionFactory();
}
public void exportTables() throws HibernateException {
Configuration cfg = new Configuration()
.addClass(Organisation.class);
new SchemaExport(cfg).create(true, true);
}
public Organisation createOrganisation(String name, List organizationList) throws HibernateException {
Organisation org = new Organisation();
org.setName(name);
org.setSuborganisations(organizationList);
org.setParentId(null);
Session session = _sessions.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(org);
tx.commit();
}
catch (HibernateException he) {
if (tx != null) tx.rollback();
throw he;
}
finally {
session.close();
}
return org;
}
/**
* Creates a suborganisation knowing only the parent organisation.
* @param parentId the parent org
* @return the child org
* @throws HibernateException
*/
public Organisation createSubOrganisation(Long parentId) throws HibernateException {
Organisation child = null;
Session session = _sessions.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Organisation org = (Organisation) session.load(Organisation.class, parentId);
child = new Organisation();
child.setName("child");
child.setParentId(org);
child.setSuborganisations(null);
org.addSubOrganisation(child);
tx.commit();
}
catch (HibernateException he) {
if (tx != null) tx.rollback();
throw he;
}
finally {
session.close();
}
return child;
}
public void deleteOrganisation(Long orgId) throws HibernateException {
Session session = _sessions.openSession();
Transaction tx = null;
Organisation org = null;
try {
tx = session.beginTransaction();
org = (Organisation) session.load(Organisation.class, orgId);
session.delete(org);
tx.commit();
}
catch (HibernateException he) {
if (tx != null) tx.rollback();
throw he;
}
finally {
session.close();
}
session = _sessions.openSession();
try {
tx = session.beginTransaction();
session.save(org);
tx.commit();
}
catch (HibernateException he) {
if (tx != null) tx.rollback();
throw he;
}
finally {
session.close();
}
}
public static void main(String[] args) {
OrganisationMain orgMain = new OrganisationMain();
try {
orgMain.configure();
orgMain.exportTables();
Organisation rootOrg = orgMain.createOrganisation("Root org", null);
Organisation childOrg = orgMain.createSubOrganisation(rootOrg.getId());
orgMain.deleteOrganisation(rootOrg.getId());
}
catch (HibernateException e) {
e.printStackTrace();
}
}
}