Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:3.0rc1
Mapping documents: see below
Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs:
Name and version of the database you are using: MySQL 4.0.18-nt
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
Using SchemaExport tool to generate tables
Using the "HibernateUtil" to open and close sessions
I initially had one object (Alert) and simply wanted to map a Map of String Key/Value pairs to a given object. I was hoping that the schemaGeneration tool would create the collections table for me and I would be left with...
Alert
--------
id collId metadata1 metadata2 ...
ie. 1 1 "foo0" "bar0"
CollectionTable
-----------
id alertID KEY VALUE
ie. 1 1 "foo0" "bar0"
2 1 "foo1" "bar1"
where collID would map to alertID and all would be well in the world.
Failing this I decided to make second Class that embodied my Map (AlertAction). Now an Alert class would have a List of AlertActions (probably better anyway).
Given this setup I was hoping to be able to define my Alert.hbm.xml to be (see
1 below). Then I simply create the AlertAction Class to hold columns for the alert_id back to the alert, and index for the list, and whatever other props I need on the object. (see
2 and 3).
The end result is that I now get NO AlertAction table created (where I did without the association).
1.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="Alert" table="Alert">
<!-- A 32 hex character is our surrogate key. It's automatically
generated by Hibernate with the UUID pattern. -->
<id name="id" type="string" unsaved-value="null" >
<column name="id" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<version column="version"
name="version"
type="integer"
access="property"
unsaved-value="undefined"/>
<property name="title">
<column name="title" length="255" not-null="true"/>
</property>
<property name="priority"/>
<property name="body"/>
<list name="actions">
<key column="alert_id"/>
<index column="index"/>
<one-to-many class="AlertAction"/>
</list>
</class>
</hibernate-mapping>
import java.util.List;
public class Alert implements IAlert {
private String id;
private int version;
private String title;
private int priority;
private String body;
private List actions;
public Alert() {
}
public String getId() {
return id;
}
private void setId(String id) {
this.id = id;
}
public int getVersion() {
return version;
}
private void setVersion(int version) {
this.version = version;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public List getActions() {
return actions;
}
public void setActions(List actions) {
this.actions = actions;
}
}
2
public class AlertAction {
private String id;
private int index;
private int alertID;
private String name;
private String value;
public AlertAction() {
}
private String getId() {
return id;
}
private void setId(String id) {
this.id = id;
}
private int getIndex() {
return index;
}
private void setIndex(int index) {
this.index = index;
}
private int getAlertID() {
return alertID;
}
private void setAlertID(int alertID) {
this.alertID = alertID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
3.
<hibernate-mapping>
<class name="AlertAction" table="AlertAction">
<!-- A 32 hex character is our surrogate key. It's automatically
generated by Hibernate with the UUID pattern. -->
<id name="id" type="string" unsaved-value="null" >
<column name="id" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property name="alertID"/>
<property name="index"/>
<property name="name"/>
<property name="value"/>
</class>
</hibernate-mapping>