views:

255

answers:

1

I have a domain object class that represents a table. This class has an association to another table, but the source class' property is not named the same as the target class' property, and I'm not sure how to hibernate map it.

Here is an example of the class to have the set (one CT to many R instances):

public class CT {
    // This is the property in the property-ref down below
    private String b;

    // The set of Rs I want to get - there may be none that correspond to a CT instance.
    private Set rs;
}

public class R {
    // This property is mapped to the column name below.
    private String rBc;
}

<!--Snippet of the mapping for class CT-->

 <set name="rs" lazy="true" sort="MyComparator" table="R" >
      <key column="R_COLUMN_NAME_THAT_REPRESENTS_THE_RELATIONSHIP" property-ref="b" />
      <one-to-many class="CLASS_THAT_R_IS" />
 </set>

Hibernate accepts this mapping, but when I pull up the set of Rs from a CT instance that I know should exist, I just get an empty PersistentSet.

Note that it is entirely possible that there may be none or more than one R for each CT instance. This is why I have the comparator in there - I can't figure out how to easily tell Hibernate how to do an ORDER BY clause without explicit SQL (which I'm hesitant to write in the Hibernate mapping.

Could someone help me out here?

A: 

The problem might be with the table="R" in the <set ..> mapping. This tells hibernate to use a join table in stead of looking for foreign key in the child table directly. From the naming I understand that you are not using a join table. Is that correct?

Maarten Winkels