I'm new to hibernate and I'm working on a project where I have to create a new database as well, so I'm using a forward engineering approach from classes to tables.
To create and load the database with test data at the same time, I created a class with a recursive method that reads a XML file, create the database (done automatically with the hbm2ddl.auto property) and populate the tables with data.
Here is the class that load the database:
Code:
public class TourManager {
private Session s2;
private Element rootElement;
private String domain="tour";
public static void main(String[] args) {
TourManager mgr = new TourManager();
mgr.loadDatabaseFromXml();
}
public void loadDatabaseFromXml() {
Session session = HibernateSessionFactory.currentSession();
s2 = session.getSession(EntityMode.DOM4J);
InputStream in;
try {
in = new FileInputStream("f:\\tourdb.xml");
} catch (IOException e) {
throw new IllegalArgumentException("Could not parse XML document",e);
}
SAXReader reader = new SAXReader();
Document document = null;
try {
document = reader.read(in);
} catch (DocumentException e) {
e.printStackTrace();
}
rootElement = document.getRootElement();
System.out.println("Root Element="+rootElement.getName());
recurLoadXml(rootElement);
}
public void recurLoadXml(Element elem) {
Iterator j = elem.elementIterator();
while (j.hasNext()) {
Element e=(Element) j.next();
recurLoadXml(e);
s2.replicate(domain+"." + elem.getName(), elem, ReplicationMode.OVERWRITE);
}
}
}
The XML file looks like this:
Code:
<?xml version ="1.0" encoding="ISO-8859-1"?>
<TourDb>
<id>1</id>
<Tour>
<id>1</id>
<country>Mexico</country>
<destination>Cancun</destination>
<tourname>PARADISE REEF ADVENTURE</tourname>
<categories>
<elt>Adventure</elt>
<elt>Snorkling</elt>
</categories>
</Tour>
<Tour>
<id>2</id>
<country>Mexico</country>
<destination>Cancun</destination>
<tourname>INDIANA JOE`S ATV AND SPEED BOAT</tourname>
<categories>
<elt>Adventure</elt>
<elt>Boat Ride</elt>
</categories>
</Tour>
</TourDb>
The mapping file looks like this:
Code:
<hibernate-mapping package="tour">
<class name="Tour" table="tour" optimistic-lock="none">
<id name="id" type="integer" unsaved-value="null" column="id">
<generator class="native"/>
</id>
<property name="tourid" type="long" column="tourid"/>
<property name="country" type="string" column="country"/>
<property name="destination" type="string" column="destination"/>
<property name="tourName" type="string" column="tour_name"/>
<array name="categories" table="tour_categories" cascade="none">
<key foreign-key="categories_id" column="categories_id"/>
<list-index>
<column name="idx" not-null="true"/>
</list-index>
<element type="string" column="elt"/>
</array>
</class>
</hibernate-mapping>
This works if categories are in a separate class/table. (Array would be replaced by a one-to-many tag in this case) But I want to store the categories in the tour_category table (generated by hb2ddl).
It looks like the replicate method only replicate entities, not relationships.
Any suggestion ?