- This topic has 5 replies, 3 voices, and was last updated 17 years, 6 months ago by logris.
-
AuthorPosts
-
deepblueliMemberI got the following relationship:
Comment (id, moderated_by) —> User (id) with “moderated_by” is a foreign key to User table
Comment has many-to-one relationship to UserWhen I tried to reverse-engineer from database to POJO and hibernate XML mapping files, “moderated_by” column is translated to “user” property in Comment POJO class. The name of the property in the POJO is according to type, not the column name from the table.
I tried to override this by manually type “moderatedBy” as the property name for the column “moderated_by” in the hibernate reverse-engineering wizard. But, it still the same.
Anyone can help me out?
Thanks!
Haris PecoMemberdeepblueli,
Could you send your table scripts (rigth click on table and Generate>DDL) ?
Regards
deepblueliMemberI know the reason why it is done so.
It is because normally for many-to-one relationship, we will create something like this:
PHOTO (id, category_id) –> PHOTO_CATEGORY (id) where category_id is foreign key to PHOTO_CATEGORY.If the tool named the property as “categoryId”, it is quite weird. Therefore, the property name is named after the foreign key’s table, which is photoCategory. If you have two many-to-one relationships to the same table, (eg. updatedBy, createdBy linked to same USER table), it will named as <foreign_key_table>By<column_name>, eg. userByUpdatedBy, userByCreatedBy.
My table structure is quite complex. But it is easy to reproduce my problem. Just create one-to-many relationship and you’ll encounter the same thing.
My only concern is why I can’t use my own property name (by manually specify them in the reverse-engineering wizard).
Thanks!!
Haris PecoMemberYou can’t change collection property name in current MyEclipse.We are planing add this feature
Regards,
logrisMemberI would like to be able to customize the names of collection properties too. It is frustrating to have to use the physical database column name in my code for all one-to-many relationships.
5.1.1 was just released… could I expect to be able to customize collection names in this release? If not, what release is the feature planned for?
Thanks for any info you can provide. (And thanks for an excellent product!)
logrisMemberHere is a basic workaround that can be used beginning with MyEclipse version 5.5 m1. This will generate property names for collection and foreign key entities that honor the property name you set on page 3 of the Hibernate regular expression wizard. Create this class somewhere in your project and set the “Custom Strategy” field to this class (on page 2 of the RE wizard).
It does a pretty good job. I didn’t really take into account multicolumn keys (they will default to the old behavior), but you can add it in if you need it. Feel free to use the code however you like.
package gov.blm.ak.sps.hibernate; import java.util.List; import org.hibernate.cfg.reveng.ReverseEngineeringStrategy; import org.hibernate.cfg.reveng.ReverseEngineeringStrategyUtil; import org.hibernate.cfg.reveng.TableIdentifier; import org.hibernate.mapping.Column; public class HibernateRevengStrategy extends org.hibernate.cfg.reveng.DelegatingReverseEngineeringStrategy { public HibernateRevengStrategy(ReverseEngineeringStrategy arg0) { super(arg0); } @Override public String foreignKeyToEntityName(String keyName, TableIdentifier fromTable, List arg2, TableIdentifier toTable, List arg4, boolean uniqueReference) { List<Column> fromColumns = arg2; List<Column> toColumns = arg4; // If the fk consists of just one column, use columnToPropertyName behaviour. // columnToPropertyName will check the hibernate.reveng.xml file for a user specified name. if (fromColumns.size()==1) { return this.columnToPropertyName(fromTable, fromColumns.get(0).getName()); } return super.foreignKeyToEntityName(keyName, fromTable, fromColumns, toTable, toColumns, uniqueReference); } @Override public String foreignKeyToCollectionName(String keyName, TableIdentifier fromTable, List arg2, TableIdentifier toTable, List arg4, boolean uniqueReference) { List<Column> fromColumns = arg2; List<Column> toColumns = arg4; // Find the class name for elements of the collection and turn it into a pluralized property String baseName = ReverseEngineeringStrategyUtil.toUpperCamelCase( ReverseEngineeringStrategyUtil.simplePluralize(this.tableToClassName(fromTable))); // If unique, use the baseName. If not, tack on the property at the other end of the foreign key. if (uniqueReference) { return baseName; } else { return baseName + "For" + capitalizeFirst( foreignKeyToEntityName(keyName, fromTable, fromColumns, toTable, toColumns, uniqueReference)); } } private String capitalizeFirst( String string ) { if (string.length()>0) { String firstLetterCap = string.substring(0,1).toUpperCase(); string = firstLetterCap + string.substring(1); } return string; } }
-
AuthorPosts