Hibernate 2
MySql 4.1.12 for Win32
Hello!
I have simplified the Person class a bit (that is, I removed some stuff like age, sex etc).
A person class contains several standard data like age, sex and also optional data that can be provided. The optional data is in key-value form (email,
ddd@ddd.fi), (address, Sunset Boulevard 132) etc
This data is submitted using a web form.
I collect the submitted key-value pairs and populate a new Option object with those values. I then put all created Option objects in a Set, which I finally sotre in the Person object (using method setOption(Set) ).
The idea is that as I use hibernate to store the Person object to MySql database, it should also store the Option objects (which reside in Person class) in Option table.
I'm using Spring's getHibernateTemplate().save() for saving data.
As I understand it, this should be doable.
I guess the problem resides in my hibernate mapping file. I don't quite figure out how to accomplish this.
Here's my classes:
Code:
public class Person
{
private java.util.Set option;
public Person() {}
public void setOption(Set set) { option = set; }
public java.util.Set getOption() { return option; }
}
public class Option
{
private Id id;
private String value;
public Option() {}
public void setId(Id id) { this.id = id; }
public void setValue(String val) { value = val; }
public Id getId() { return id; }
public String getValue() { return value; }
public boolean equals(Object o) { // implementation }
public int hashCode() { // implementation }
}
public class Id
{
private int id;
private String key;
public Id() {}
public void setId(int id) { this.id = id; }
public void setKey(String key) {this.key = key; }
public int getId() { return id; }
public String getKey() { return key; }
}
Here's my hibernate mappings:
Code:
<class name="Person" class="com.jme.Person">
<id name="id" type="string" unsaved-value="null" >
<column name="id" sql-type="varchar(4)" not-null="true"/>
<generator class="native" />
</id>
<set name="options">
<key column="id" />
<one-to-many class="Options" />
</set>
</class>
<class name="Options" table="Options">
<composite-id name="id" class="com.jme.Id">
<key-property name="id" column="id" />
<key-property name="opt_key" column="opt_key" />
</composite-id>
<property name="opt_value" type="string" not-null="true">
<column name="opt_value" sql-type="text" />
</property>
</class>
[/code]