-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Saving one-to-many association
PostPosted: Tue Oct 05, 2004 5:12 pm 
Newbie

Joined: Thu Sep 16, 2004 4:35 pm
Posts: 6
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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 05, 2004 5:38 pm 
Newbie

Joined: Thu Sep 16, 2004 4:35 pm
Posts: 6
I have not fixed the problem, but I think I was missing an argument for inverse in the mapping of the channel's set of broadcasts:


<set name="broadcasts" inverse="true" lazy="true">
<key column="channel_id"/>
<one-to-many class="Broadcast" />
</set>

This is supposed to be a bidirectional association.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.