When importing xml to a DB with Hibernate, is there a way to resolve an attribute consisting of comma-separated values to populate related table(s)?
In this (somewhat obfuscated) example I have an xml file, each row of which represents a Person. The Person has a Hobbies property which contains a comma-separated list of values. The Person-Hobby relationship is many to many. In reality I have gigs of data to process.
When importing each Person to the PEOPLE table, I would like to add each Hobby to the HOBBIES table (ignoring duplicates), then add a mapping to the PEOPLE_HOBBIES table.
I've set up my mapping files with bi-directional associations and Hibernate appears to construct the tables as I'd expect (details below), however I don't see what mechanism I can use for extracting/populating the HOBBIES and PEOPLE_HOBBIES while processing PEOPLE.
All help and/or RTFM references gratefully received.
This is the file I'm processing (people.xml):
<People>
<Person Id="1" Name="Dave" Hobbies="drinking, walking"/>
<Person Id="2" Name="Geoff" Hobbies="football, ballet"/>
<Person Id="3" Name="Anne" Hobbies="walking, karate"/>
<Person Id="4" Name="Frank" Hobbies="karate, cross-stitch"/>
</People>
The Person.hbm.xml is (omitting xml decl):
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="name.seller.rich.hobby">
<class name="Person" node="Person" table="PEOPLE">
<id name="id" node="@Id" column="PEOPLE_ID"/>
<property name="name" node="@Name" column="NAME" type="string"/>
<property name="hobbies" node="@Hobbies" column="HOBBIES" type="string"/>
<set name="hobbiesSet" table="PEOPLE_HOBBIES">
<key column="PEOPLE_ID"/>
<many-to-many column="HOBBY" class="Hobby"/>
</set>
</class>
</hibernate-mapping>
The Hobby.hbm.xml is:
<hibernate-mapping package="name.seller.rich.hobby">
<class name="Hobby" node="Hobby" table="HOBBIES">
<id name="hobby" column="HOBBY" type="string"/>
<set name="people" table="PEOPLE_HOBBIES" inverse="true">
<key column="HOBBY"/>
<many-to-many column="PEOPLE_ID" class="Person"/>
</set>
</class>
</hibernate-mapping>
This is the Person class, in the setHobbies() method I populate the hobbiesSet with Hobby instances:
package name.seller.rich.hobby;
import java.util.HashSet;
import java.util.Set;
public class Person {
private long id;
private String name;
private String hobbies;
private Set hobbiesSet = new HashSet();
public String getHobbies() {
return hobbies;
}
public Set getHobbiesSet() {
if (hobbiesSet == null) {
hobbiesSet = new HashSet();
}
return hobbiesSet;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
public void setHobbies(final String hobbies) {
this.hobbies = hobbies;
}
public void setHobbiesSet(final Set hobbiesSet) {
this.hobbiesSet = hobbiesSet;
}
public void setId(final long id) {
this.id = id;
}
public void setName(final String name) {
this.name = name;
}
}
This is the code I'm using to process the file:
package name.seller.rich.hobby;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.hibernate.EntityMode;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class DataImporter {
public static void main(final String[] args) {
File baseDir = new File("C:\\workspaces\\hobby");
DataImporter importer = new DataImporter();
Configuration config = importer.setupDb(baseDir);
if (config != null) {
importer.importContents(new File(baseDir, "people.xml"), config);
}
}
private void importContents(final File file, final Configuration config) {
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Session dom4jSession = session.getSession(EntityMode.DOM4J);
SAXReader saxReader = new SAXReader();
try {
Document document = saxReader.read(file);
List list = document.selectNodes("//Person");
Iterator iter = list.iterator();
while (iter.hasNext()) {
Object personObj = iter.next();
dom4jSession.save(Person.class.getName(), personObj);
}
session.flush();
tx.commit();
session.close();
} catch (HibernateException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}
private Configuration setupDb(final File baseDir) throws HibernateException {
Configuration cfg = new Configuration();
cfg.addFile(new File(baseDir, "name/seller/rich/hobby/Person.hbm.xml"));
cfg.addFile(new File(baseDir, "name/seller/rich/hobby/Hobby.hbm.xml"));
SchemaExport export = new SchemaExport(cfg);
export.setOutputFile("hobbyDB.txt");
export.execute(false, true, false, false);
return cfg;
}
}
This is the resulting content in the PEOPLE table.
PEOPLE_ID |NAME |HOBBIES
-------------------------------------------------------
1 |Dave |drinking, walking
2 |Geoff |football, ballet
3 |Anne |walking, karate
4 |Frank |karate, cross-stitch
...and these are the empty HOBBIES and PEOPLE_HOBBIES tables:
HOBBIES:
HOBBY
----------------------
0 rows selected
PEOPLE_HOBBIES:
PEOPLE_ID |HOBBY
---------------------------------------
0 rows selected