Many-to-many relationship with attributes in Hibernate

M

Mirko

Hi,
I'm trying to define a simple many-to-many relationship (with
attributes) called AB between 2 tables A and B.

A <--- AB <----> B

I also need to retrieve all the records in AB that are related to B,
but unfortunately when I execute the following:

Set ab = objectFromB.getABrelationships()

it returns only a subset of the objects in AB that are related to B.
Not only I can see that the objects in AB related to B are more by
looking at the database, but even executing a count via Hibernate
returns a higher number of elements than those in the set ab:

session.createQuery("SELECT COUNT(*) FROM package.classRelationshipAB
where objectB = <something>");

I feel the problem is in the mapping file, but I can't see what's
wrong.
Any idea or working examples very much appreciated.
Below is the mapping file:

<class name="package.classA" table="TABLE_A">
<meta attribute="class-description">
</meta>
<id name="id" type="java.lang.Integer" column="tableA_id">
<meta attribute="scope-set">protected</meta>
<generator class="native"/>
</id>
<property name="attribute1" type="java.lang.Integer">
<meta attribute="field-description">useful attribute</meta>
</property>
</class>

<class name="package.classB" table="TABLE_B">
<id name="id" type="java.lang.Integer" column="tableB_id">
<meta attribute="scope-set">protected</meta>
<generator class="native"/>
</id>
<property name="attribute2" type="java.lang.Double">
<meta attribute="field-description"/>
</property>
<set name="ABrelationships" inverse="true" lazy="true">
<key column="tableB_id"/>
<one-to-many class="package.classRelationshipAB"/>
</set>
</class>


<class name="package.classRelationshipAB" table="AB_RELATIONSHIP">
<id name="id" type="java.lang.Integer" column="ab_relationship_id">
<meta attribute="scope-set">protected</meta>
<generator class="native"/>
</id>
<property name="relationship_attribute" type="java.lang.Double">
<meta attribute="use-in-tostring">true</meta>
</property>
<many-to-one name="object_b" column="tableB_id" class="package.classB"
not-null="true"/>
<many-to-one name="object_a" column="tableA_id" class="package.classA"
not-null="true"/>
</class>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top