-->
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.  [ 3 posts ] 
Author Message
 Post subject: n:m relationsship and empty collection
PostPosted: Mon Jun 18, 2007 6:36 am 
Newbie

Joined: Mon Jun 18, 2007 6:30 am
Posts: 2
Location: Braunschweig, Germany
Hi,

I have a strange problem. I currentyl try to make a n:m relaationsship between two classes / tables THING and ACTION

One THING contains one or more actions, while an ACTION can be assigned to different THINGS.

So, I have a getter / setter in my THING class

public Collection getActions() {... }

Working with annotations, I added the following stuff at the getter:

@ManyToMany(
targetEntity=de.lerascura.ActionPO.class,
cascade={CascadeType.PERSIST, CascadeType.MERGE})
@JoinTable(
name="TBLACTIONREF",
joinColumns={
@JoinColumn(name="IMPORTER"),
@JoinColumn(name="NUMBER"),
@JoinColumn(name="YEAR")
},
inverseJoinColumns={
@JoinColumn(name="ACTION")
})

I start my program and it says "Here's your empty collection".. Nice. I forced Hibernate to output the statement and copy pasted it into a SQL editor. Everythings fine. I get a bunch of records, so also hibernate should get them, but instead the above mentioned getter just returns an empty collection?

Any idea what's going wrong here?


Best regards,
Andreas


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 19, 2007 3:22 am 
Newbie

Joined: Mon Jun 18, 2007 6:30 am
Posts: 2
Location: Braunschweig, Germany
Okay, some more information: The problem only occurs, if the THING table has a composite ID---

As I mentioned before: The SQL statement that is created by hibernate works fine, but the data are not inserted into the Collection... hmm..

I'm helpless..


Top
 Profile  
 
 Post subject: Collections and composite ids
PostPosted: Thu Jul 05, 2007 5:00 pm 
Newbie

Joined: Wed Jan 17, 2007 4:07 pm
Posts: 3
I think I'm having the same problem you are. We have two tables, Items and Pricing. Pricing has a foreign key relation to Items. Both tables have a two-part primary key. We have a class called PrimaryKey . The classes are as such (I won't put everything, just what is important).

public class Item
{
//a bunch of properties
private Collection pricings;

//getters and setters
}

public class Pricing
{
//a bunch of properties
private A item;

//getters and setters
}

public class PrimaryKey
{
private Integer pk;
private Integer cpk;
//getters and setters
}


Here is the Item mapping:
<hibernate-mapping>
<class name="daoBroker.pojo.Item" table="Items" dynamic-update="false">
<id name="id" type="hibernateDAOBroker.dao.PrimaryKeyType">
<column name="invPk" sql-type="integer"/>
<column name="invCpk" sql-type="integer"/>
<generator class="hibernateDAOBroker.IDGenerator"/>
</id>
<!-- a bunch of properties -->
<bag name="pricing" table="Pricinginverse="true" cascade="all">
<key>
<column name="invPldFK"/>
<column name="invPldCFK"/>
</key>
<one-to-many class="daoBroker.pojo.Pricing"/>
</bag>
</class>
</hibernate-mapping>

Here is the Pricing mapping:

<hibernate-mapping>
<class name=".daoBroker.pojo.Pricing" table="Pricing">
<id name="id" type="hibernateDAOBroker.dao.PrimaryKeyType">
<column name="pldPk" sql-type="integer"/>
<column name="pldCPk" sql-type="integer"/>
<generator class="hibernateDAOBroker.IDGenerator"/>
</id>
<!-- a bunch of properties -->
<many-to-one name="item class="daoBroker.pojo.Item">
<column name="invPldFK"/>
<column name="invPldCFK"/>
</many-to-one>
</class>
</hibernate-mapping>

Saving works fine, such as:

Item item = new Item();
//set properties
Pricing pricing = new Pricing();
//set properties

//add pricing to the item's collection
item.addPricing(pricing);

//all session functionality is wrapped in this dao object
dao.save(item);

Since we provide our own IDGenerator class, Hibernate is even generating our two part keys for us and setting the ids for all objects correctly.

When we try to do the following is when we run into trouble:

Item item = (Item) dao.getById(new PrimaryKey(Integer.valueOf(2), Integer.valueOf(1000));

Collection pricings = item.getPricingList();

//pricings is empty

The really strange thing is that the sql to obtain the pricing records is executed and if I put debug in the setter methods and constructor of the Pricing class, they print. The correct values are being passed to the setter methods and the objects are populated properly, but when you access the collection they aren't there! It's like hibernate is creating and populating everything, but never adding them to the collection.

I've changed the fetch strategy and it doesn't matter, lazy and eager fetching both result in an empty collection. I've also tried using sets instead of bags. Even tried writing my own PersistentCollection.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.