1

I have a hierarchy of objects in hibernate. I am simply printing the hierarchy out by looping through the hierarchy.

Everything prints out great until I get to the bottom of the hierarchy. I have a Group object which has a collection of Artifact objects. I set up by hibernate mapping to retrieve a Set of artifacts for the Group object. I did this on a column called ratio as opposed to the id column. The Group object has 1 to many relationship with the artifacts. Both objects have the ratio field.

The Group mapping looks like so:

<hibernate-mapping>
    <class name="Group" table="Group">
        <id name="id" type="int">
            <generator class="native" />
        </id>
        <property name="ratio" type="string"></property>
          <set name="artifacts" table="Artifact" 
                inverse="true" lazy="true" fetch="select">
            <key>
                <column name="ratio" not-null="true" />
            </key>
            <one-to-many class="Artifact" />
        </set>
    </class>
</hibernate-mapping>

The Artifact mapping:

<hibernate-mapping>
    <class name="Artifact" table="Artifact">
        <id name="id" type="int">
            <generator class="native" />
        </id>
        <property name="ratio" type="string"></property>
    </class>
</hibernate-mapping>

The problem is that group objects print out, but the artifacts do not return anything in the queries. I have printed the ratio returned from each group. I have then copied the sql statement out put hibernate outputs and substituted the ratio in the where statement criteria and everything returns correctly:

 select artifact0_.ratio as rat2_3_1_, artifact0_.id as id1_, artifact0_.id as id4_0_, artifact0_.ratio as rat2_4_0_from Artifact artifact0_ where artifact0_.ratio='1'

Does anyone know why the hibernate collection is not returning anything?

Atma
  • 29,141
  • 56
  • 198
  • 299

1 Answers1

1

If your statement executes correctly in sql with the ratio parameter substituted but is does not do when executed by Hibernate, then this is because you've substituted a different value than Hibernate actually has. You can use a tool like log4jdbc to see the actual values of the parameters in the generated SQL statement.

The problem in your mapping is: In the set you define the key as ratio. This makes ratio the foreign key in the table Artifact and joins it with the key of the parent, which is Group.id (and not Group.ratio as you've probably intended).

There are two solutions:

  1. You define ratio as the unique key in Group. Then in your mapping Artifact.ratio is joined to Group.ratio.

  2. If you can't do 1. because ratio is not unique in Group (or if you do not want to do 1.), then the relation between Group and Artifact is a many to many relation, and you should use the <many-to-many> tag. Here you have the properties column and property-ref to name the join columns in both tables.

Johanna
  • 5,223
  • 1
  • 21
  • 38
  • Ratio is a unique key in the Group table. Is there something I can do in the mapping to ensure that they both join on ratio? – Atma Jan 30 '13 at 19:54
  • Then you can define ´ratio´ as the primary key. Or you use many-to-many even if it is logically only a one-to-many relation. Or you do not define the relation in Hibernate and in the method `Group.getArtifactList()` you manually load the artifacts in a separate HQL statement. – Johanna Feb 04 '13 at 11:18