Hello,
I have a problem using Hibernate. I have two classes, one is Client, the other is Hobby. Every client can have several hobbies. The data is read in from an XML file. Im trying to use session.saveOrUpdate(client); But i get this error. My database at the moment is empty, I want the code to insert if the object does not exist in database and update if it does, the ID can be the clients name, for hobby, the hobby name. My mapping:
client.hbm.xml
Code:
<class name="Client" table="clients">
<id name="Name" type="string" column="cl_id" unsaved-value="0">
<generator class="assigned"/>
</id>
<property name="Name">
<column name="name" />
</property>
<property name="Address">
<column name="address" />
</property>
<property name="DateOfBirth">
<column name="dob" />
</property>
<set name="hobbies" table="cl_hobby" cascade="save-update,persist">
<key column="cl_id"/>
<many-to-many column="hobby_id" class="Hobby"/>
</set>
</class>
hobby.hbm.xml
Code:
<class name="Hobby" table="hobbies">
<id name="Name" type="string" column="hobby_id" unsaved-value="0">
<generator class="assigned"/>
</id>
<property name="Name">
<column name="name" />
</property>
</class>
My code for main:
Code:
Mapping mapping = new Mapping();
Configuration cfg = new Configuration();
cfg.configure();
SessionFactory sessFactory = cfg.buildSessionFactory();
try {
mapping.loadMapping("book-mapping.xml");
FileReader reader = new FileReader("book.xml");
Unmarshaller unmarshaller = new Unmarshaller(Clients.class);
unmarshaller.setMapping(mapping);
Clients book = (Clients)unmarshaller.unmarshal(reader);
List clients = book.getClients();
Iterator iter = clients.iterator();
while ( iter.hasNext() ) {
Client person = (Client) iter.next();try{
Session session = sessFactory.openSession();
Transaction tx = session.beginTransaction();
session.saveOrUpdate(person);
tx.commit();
session.close();
} catch(Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace(System.err);
}
The Hobby and Client has it's setters and getters, for name's and everything else. Any ideas?Thanks!