HiberObjects

Download

Order

Forum

Vote for this plugin

It is currently Fri Sep 03, 2010 5:57 pm

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Add tag to change column name but foreign key does not chang
PostPosted: Fri Jan 22, 2010 9:34 am 
Offline

Joined: Fri Jan 15, 2010 9:43 am
Posts: 46
This has also become one of my highest priority issues that I would like fixed before others I've raised because now I have an incorrectly generated hbm.xml file that needs to be manually edited each time I regenerate my code.

I have classes foo1 and foo2. foo1 has a foreign key to foo2. foo2 had a default column name of foo2_id but I changed it to foo2id. I expected the foreign key on foo1 to automatically refer to foo2id instead of foo2_id. To workaround this I added a hibernate tag on the relationship between foo1 and foo2 but that is cumbersome to do for every relationship.

Can this be corrected?

Thanks.


Last edited by skurlow on Tue Jan 26, 2010 9:43 am, edited 1 time in total

Report this post
 
 Profile  
Reply with quote  
 Post subject: Re: Add tag to change column name but foreign key does not chang
PostPosted: Mon Jan 25, 2010 12:58 pm 
Offline

Joined: Tue Apr 10, 2007 4:42 pm
Posts: 561
Location: Uppsala, Sweden
Hi,

Do I understand you correctly if I think that you added a @Column tag to the id attribute in class foo2 to change its column name, and then you want the foreign key to use that @Column tag?

Regards,
Lars


Report this post
 
 Profile  
Reply with quote  
 Post subject: Re: Add tag to change column name but foreign key does not chang
PostPosted: Tue Jan 26, 2010 6:40 am 
Offline

Joined: Fri Jan 15, 2010 9:43 am
Posts: 46
Yes your understanding is correct. In my scenario foo1 is called ScanStock and foo2 is called TimeframeDefinition. Here are their hbm.xml files:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!-- Generated by HiberObjects. DO NOT EDIT! -->
<hibernate-mapping>
<class name='com.pinkwater.trader.server.ScanStock' table='scanstoc'>
<id name='id' type='long' column='scanstocid'>
<generator class='native' />
</id>
<set name='bar' table='tb_scan_stock_bar'>
<key column='scanstocid' foreign-key='fk_scan_stock_bar_key'/>
<many-to-many class='com.pinkwater.trader.server.Bar' foreign-key='fk_scan_stock_bar' column='bar_id' />
</set>
<set name='scannedParameter' table='tb_scan_stock_scan_parameter'>
<key column='scanstocid' foreign-key='fk_scan_stock_scan_parameter_key'/>
<many-to-many class='com.pinkwater.trader.server.ScanParameter' foreign-key='fk_scan_stock_scanned_parameter' column='scanned_parameter_id' />
</set>
<many-to-one name='stockDefinition' class='com.pinkwater.trader.server.StockDefinition' not-null='true' cascade='save-update' foreign-key='fk_scan_stock_stock_definition_id'>
<column name='stock_definition_id'/>
</many-to-one>
<many-to-one name='timeframeDefinition' class='com.pinkwater.trader.server.TimeframeDefinition' not-null='true' cascade='save-update' foreign-key='fk_scan_stock_timeframe_definition_id'>
<column name='timeframe_definition_id'/>
</many-to-one>
</class>
</hibernate-mapping>

and:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!-- Generated by HiberObjects. DO NOT EDIT! -->
<hibernate-mapping>
<class name='com.pinkwater.trader.server.TimeframeDefinition' table='timefrmdefn'>
<id name='id' type='long' column='timefrmdefnid'>
<generator class='native' />
</id>
<property name='description' type='string' column='description'>
</property>
</class>
</hibernate-mapping>


Report this post
 
 Profile  
Reply with quote  
 Post subject: Re: Add tag to change column name but foreign key does not chang
PostPosted: Tue Jan 26, 2010 9:40 am 
Offline

Joined: Fri Jan 15, 2010 9:43 am
Posts: 46
I tried to add a Hibernate tag on the association to change the column name from scan_id to scanid but this only changed the ScanPage.hbm.xml but I expected the Scan.hbm.xml to similarly change. Now Scan.hbm.xml is incorrect and HibernateHelper fails to load scan.hbm.xml, the two hbm.xml files follow:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!-- Generated by HiberObjects. DO NOT EDIT! -->
<hibernate-mapping>
<class name='com.pinkwater.trader.server.Scan' table='scan'>
<id name='id' type='long' column='scanid'>
<generator class='native' />
</id>
<property name='tradingDate' type='timestamp' column='traddate'>
</property>
<many-to-one name='scanDefinition' class='com.pinkwater.trader.server.ScanDefinition' not-null='true' cascade='save-update' foreign-key='fk_scan_scan_definition_id'>
<column name='scan_definition_id'/>
</many-to-one>
<set name='scanPages' inverse='true'>
<key column='scan_id' foreign-key='fk_scan_page_scan_key'/>
<one-to-many class='com.pinkwater.trader.server.ScanPage' />
</set>
</class>
</hibernate-mapping>

and:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!-- Generated by HiberObjects. DO NOT EDIT! -->
<hibernate-mapping>
<class name='com.pinkwater.trader.server.ScanPage' table='scanpage'>
<id name='id' type='long' column='scanpageid'>
<generator class='native' />
</id>
<property name='pageNumber' type='integer' column='pagenumb'>
</property>
<set name='scannedStocks' inverse='true'>
<key column='scan_page_id' foreign-key='fk_scan_stock_scan_page_key'/>
<one-to-many class='com.pinkwater.trader.server.ScanStock' />
</set>
<property name='filename' type='string' column='filename'>
</property>
<many-to-one name='scan' class='com.pinkwater.trader.server.Scan' not-null='true' cascade='save-update' foreign-key='fk_scan_page_scan_id' column='scanid'>
</many-to-one>
</class>
</hibernate-mapping>


Report this post
 
 Profile  
Reply with quote  
 Post subject: Re: Add tag to change column name but foreign key does not chang
PostPosted: Thu Jan 28, 2010 10:22 pm 
Offline

Joined: Tue Apr 10, 2007 4:42 pm
Posts: 561
Location: Uppsala, Sweden
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


Report this post
 
 Profile  
Reply with quote  
 Post subject: Re: Add tag to change column name but foreign key does not chang
PostPosted: Mon Feb 01, 2010 1:58 pm 
Offline

Joined: Fri Jan 15, 2010 9:43 am
Posts: 46
Hi Lara,

I downloaded the hbm.template and modified it according to your post but the code generation is still the same so I am guessing that I have placed the file in the wrong package???

From a usability point of view it seems natural that if I change the column name for an attribute that all foreign keys that utilise it should also use the new column name. Can you make that an automatic feature in the next release?

Thanks,
Stephen


Report this post
 
 Profile  
Reply with quote  
 Post subject: Re: Add tag to change column name but foreign key does not chang
PostPosted: Mon Feb 01, 2010 9:58 pm 
Offline

Joined: Tue Apr 10, 2007 4:42 pm
Posts: 561
Location: Uppsala, Sweden
Hi Stephen,

If you upgraded to 2.0.7 you will get the new hbm.template in there, so you don't need to place it in your project. So this will be the default behavior for all projects that don't override the hbm.template. But the right place to put it is in the project root.

Have you tried Save All (in the HiberObjects popup menu) after upgrading/changing hbm.template?

If not, perhaps I didn't understand exactly what you're after. Could you post the exact classes and tags you have designed (or mail a screenshot to eclipse@objectgeneration.com) and the exact XML you want generated.

/Lars


Report this post
 
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC + 1 hour [ DST ]


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2002, 2006 phpBB Group