I am trying to start out with a bi-directional one-to-many assocation, similar to the category-items example in Hibernate in Action. My association is between Channels (one) and Broadcasts (many). While still dealing with the objects in a transient state, I get an exception when I try to add to the set of broadcasts. I have listed the classes, mappings and exception below.
Here are the classes:
package com.px.ecm.domain;
public class Broadcast {
long id;
String name;
Channel channel;
public Channel getChannel() {
return channel;
}
public void setChannel(Channel channel) {
this.channel = channel;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.px.ecm.domain;
import net.sf.hibernate.collection.Set;
public class Channel {
long id;
String name;
Set broadcasts = null;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set getBroadcasts() {
return broadcasts;
}
public void setBroadcasts(Set broadcasts) {
this.broadcasts = broadcasts;
}
}
Here are the mappings:
<hibernate-mapping package="com.px.ecm.domain">
<class name="Broadcast" table="broadcast">
<id name="id" type="long" column="broadcast_id" unsaved-value="0">
<generator class="native"/>
</id>
<property name="name" column="name" type="string" length="30" not-null="true"/>
<many-to-one
name="channel"
column="channel_id"
class="Channel"
not-null="true" />
</class>
</hibernate-mapping>
<hibernate-mapping package="com.px.ecm.domain">
<class name="Channel" table="channel">
<id name="id" type="long" column="channel_id" unsaved-value="0">
<generator class="native"/>
</id>
<property name="name" column="name" type="string" length="30" not-null="true"/>
<set name="broadcasts" lazy="true">
<key column="channel_id"/>
<one-to-many class="Broadcast" />
</set>
</class>
</hibernate-mapping>
I am trying to get this working as a standalone class:
public class ECMHibernateMain {
public static void main(String[] args){
try {
Session session = HibernateUtil.currentSession();
Transaction tx= session.beginTransaction();
Channel channel = new Channel();
channel.setName("Test channel");
Set broadcasts = new Set();
Broadcast broadcast1 = new Broadcast();
broadcast1.setName("Test 1");
Broadcast broadcast2 = new Broadcast();
broadcast1.setName("Test 2");
Broadcast broadcast3 = new Broadcast();
broadcast1.setName("Test 3");
broadcasts.add(broadcast1);
broadcasts.add(broadcast2);
broadcasts.add(broadcast3);
channel.setBroadcasts(broadcasts);
broadcast1.setChannel(channel);
broadcast2.setChannel(channel);
broadcast3.setChannel(channel);
session.saveOrUpdate(channel);
tx.commit();
HibernateUtil.closeSession();
} catch(MappingException me) {
me.printStackTrace();
} catch(HibernateException he) {
he.printStackTrace();
}
}
}
but I get this exception:
16:14:25,458 ERROR LazyInitializationException:25 - Failed to lazily initialize a collection - no session or session was closed
net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no session or session was closed
at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:209)
at net.sf.hibernate.collection.PersistentCollection.write(PersistentCollection.java:84)
at net.sf.hibernate.collection.Set.add(Set.java:154)
at com.px.ecm.ECMHibernateMain.main(ECMHibernateMain.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:78)
I have seen mentions in posting of having to associate objects with the session using a lock. I do not see this mentioned in the Hibernate in Action sections on associations.
thanks for your help,
Michel
mbrudzinski@yahoo.com