Hi,
I think I have fixed what you want. Please try it by modifying hbm.template (see the following post) as follows:
Code:
// For map, set, list and bag, generate <key column='...'>
if(['map', 'set', 'list', 'bag'].contains(element) && !childIndex.containsKey('key')) {
def idCol = idColumn(c, null)
if(inverse != null) {
def inverseNaturalId = findNaturalId(c)
String col = (inverseNaturalId != null ? inverseNaturalId.name : 'id')
idCol = mixedCase2UnderScore(inverse.name) + '_' + col
}
def fk = 'fk_' + idCol
%> <key column='${idCol}' foreign-key='${fk}'/>
<% }
This is the Groovy code that generates the foreign-key for set.
If I then create a @hibernate tag with column=custid on the Customer id attribute, I get the following hbm.xml:
Code:
<hibernate-mapping>
<class name='com.xyz.myproject.model.Customer' table='tb_customer'>
<id name='id' type='long' column='custid'>
<generator class='native' />
</id>
<set name='tickets'>
<key column='custid' foreign-key='fk_custid'/>
<one-to-many class='com.xyz.myproject.model.Ticket' />
</set>
</class>
</hibernate-mapping>
I know it is a steep learning curve to modify this template, but if you want full control of the generated database schema, this is how to do it.
My personal view is that these details aren't so important, as long as we can map the database to Java objects. I think these Hibernate features are mostly meant for mapping existing database schemas to Java, not when we generate the database from the Java classes. But I'm sure all DBA's will disagree with me ;-)
Regards,
Lars