I have a class A
that has a set of B
's. However, these two objects are linked by fields that are NOT the primary key.
For B
, I can use <key column>
, but how do I specify that the join should be in A
.secondary_column
? Not A
.table_primary_key_id
?
<class table="a">
<id column="table_primary_key_id">
</id>
<property column="secondary_column" />
<set table="B" lazy="false" >
<key column="B_not_primary" />
<one-to-many class="BClass" />
</set>
</class>
Hibernate requires that entity tables have primary keys.
No, Hibernate will not work without primary key. Every table should have some unique key. The mathematical definition of a relation is a set of tuples.
The type attribute holds the hibernate mapping type, this mapping types will convert from Java to SQL data type. The <many-to-one> element is used to set the relationship between EMPLOYEE and ADDRESS entities. The name attribute is set to the defined variable in the parent class, in our case it is address.
Foreign key refers to single column or group of columns in table that link data present in another table through its primary key. A Foreign key can't exist without its parent key but viceversa is not true. Want to be a Hibernate Master ?
Solved with
<set name="someSet" table="B" lazy="false">
<key column="B_not_primary" property-ref="secondary_column" />
<one-to-many class="BClass" />
</set>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With