-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 19 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Extending all domain model classes from a base class
PostPosted: Tue Oct 17, 2006 12:21 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
All of my domain model POJOs should extend a common base class. There are common single-field (different column names though) primary keys and auditing information in every table, so this obviously makes sense. What I am curious about, is how are people accomplishing this functionality when reverse engineering/pojo generating, since there are a few ways to do this.
  1. Override PojoTypeDeclaration.ftl to explicitly set the extends phrase.
  2. Override id.hbm.ftl and/or property.hbm.ftl to add <meta attribute="gen-property">false</meta> to the hbm.xml files.
  3. Manually add meta attributes to the hbm.xml files after reverse engineering.
  4. Using some sort of inheritance mapping.

Option #3 is not possible because there are over 430 tables in this application. At the moment, I am using option #2, but did originally use option #1. I have not yet looked at option #4, but that could be a possibility since there are lots of places in the templates where a pojo.isSubclass() is a conditional.

An ancilliary question is: how to properly generate the constructors. Of course, I can do this by modifying yet another template (PojoConstructors.ftl), but it still seems as if I am overlooking something fundamental. Thanks in advance for your input.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 17, 2006 12:49 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Whoops, forgot to mention #5, using a custom ReverseEngineeringStrategy and overriding the tableToMetaAttributes and columnToMetaAttributes methods. I am in the middle of trying this one out to see how it feels.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 17, 2006 1:16 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
#6 use latest svn and put meta attributes in reveng.xml or a custom reverse engineering strategy.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 17, 2006 1:26 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Very odd that I just happened to check out the latest svn yesterday and coincidentally there exists two sparkly new methods in the ReverseEngineeringStrategy for me to use for just this purpose (hence the inital follow up post). You better cut that out, you're giving me the heebie-jeebies with that extra-sensory perception!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 18, 2006 2:34 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
A quick follow-up question/request.

Before you make another release, would you be amenable to changing the method signatures of at least the two methods concerning meta attributes in the ReverseEngineeringStrategy interface? It would potentially be more helpful to have the org.hibernate.mapping.Table object as the passed-in parameter instead of the org.hibernate.cfg.reveng.TableIdentifier. The data in the TableIdentifier is contained in the Table class. This would allow, for instance, the generation of specific meta attributes with conditionals based on primary keys only, especially when the primary key name cannot be determined by the table name alone. Another example of a potential benefit of this change is to allow the developer to generate a 'class-description' meta attribute whose value could be the based on the Table.getComment().

There may be problems with this change in that I have not done any impact analysis on tightly integrated parts of the system (using the xml file instead of the class override, any potential plugin problems, etc.) There may, of course, be other reasons you did not pass the Table object, memory leaks, speed issues. Note that the isManyToManyTable method already passes the Table object, so this is not unprecendented.

I can easily make the necessary code changes if you point me in the direction of how to go about doing so. Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 19, 2006 2:34 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
I thought long and hard before i added Table directly into the isManyToMany method.

the issue is that alot of the methods of revengstrategy is used when the configuration object is beeing built and thus it is not complete and not safe to access.

In the case of manytomany it will only be used after all tables are created so not so big an issue here.

I will have to see if that goes for the metaattribute methods too.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 20, 2006 10:40 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
For now I have a workaround by overriding the isManyToManyTable and storing the passed Table object in an instance variable in my ReverseEngineeringStrategy. Then I use the stored instance of the Table to find the primary key matches in the overridden columnToMetaAttributes. This will work primarily because isManyToManyTable is the first method called in the JDBCBinder's createPersistentClasses, so it is guaranteed to be called before columnToMetaAttributes.

Note, however, that the tables that I am working with are first normal form, with no many-to-many relationships and single field primary keys (the DBAs have been working on this schema for about 1.5 years now).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 25, 2007 9:06 am 
Newbie

Joined: Wed Nov 08, 2006 8:50 am
Posts: 13
Could you please send me or point me to an example? I am a little bit overwhelmed, still confused about what I must change in order to accomplish this...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 25, 2007 9:52 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
1) Inheriting from a base class using a ReverseEngineeringStrategy:

Code:
   @Override
   public Map tableToMetaAttributes(TableIdentifier tableIdentifier) {
      Map<String, MetaAttribute> metaAttributes = new HashMap<String, MetaAttribute>();

      // Generate the class-code meta attribute to add a default
      // serialVersionUID
      MetaAttribute classCode = new MetaAttribute("class-code");
      classCode.addValue("private static final long serialVersionUID = 1L;");
      metaAttributes.put("class-code", classCode);

      // Generate the extends meta attribute to extend all domain classes from
      // BaseModel
      MetaAttribute extendsCode = new MetaAttribute("extends");
      extendsCode.addValue("BaseModel");
      metaAttributes.put("extends", extendsCode);

      return metaAttributes;
   }


If you need more info, lemme know.


Top
 Profile  
 
 Post subject: where can i find the meta-attributes
PostPosted: Fri Jan 26, 2007 9:52 am 
Newbie

Joined: Wed Nov 08, 2006 8:50 am
Posts: 13
Is there a place where i can find these attributes?

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 26, 2007 9:53 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
yes, in the docs.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 26, 2007 4:12 pm 
Newbie

Joined: Wed Nov 08, 2006 8:50 am
Posts: 13
Ok, I found it, was very easy... My fault being so lazy...

About the docs, I downloaded the 3.2.0b9 and could not found neither the source or javadoc for the hibernate tools itself. Is really difficult for me to know what each method is expected to do, and what which parameter means without the javadoc. Is there a different place where I could download the code/javadoc?
Inside the doc.zip folder, which is where I thought I would find it, ther is only the Hibernate doc, not the tools. I can't find, for instance, the JavaDoc of the DelegatingReverseEngineeringStrategy class...

I thought that maybe it would be in the doc folder within hibernate-tools.jar, but it is not

I am not being lazy this time, I have traversed the whole zip file and have not found it...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 26, 2007 5:11 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Checkout HibernateExt from the SVN repository. See http://hibernate.org/268.html for the basics, but use
Code:
svn co http://anonsvn.jboss.org/repos/hibernate/branches/Branch_3_2/HibernateExt
to checkout the code instead of what it says on 268.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 29, 2007 3:31 pm 
Newbie

Joined: Wed Nov 08, 2006 8:50 am
Posts: 13
I have the same structure than you, all my entity classes are inherited from a base class, which has 4 base properties, one for the primary key, two for auditing and one for description.

Anyway, I've succesfully renamed the ordinary columns for my super properties, but I could only change the name of the properties, I would like to change the method itself, making it return super.dosomething(). Is it doable via a custom reverse engineering strategy?

Is it also possible to supress the setter methods for these properties (I need the getters because that's where the annotations go, but the setters are the same as the super.

Also, I would like that all the represented PK's on the classes (ids) would have its property renamed to "id" (without the setters again, since they're all the same type and it's already on the super class), even though there are differente column names. You've mentioned it on your early posts, but how did you do it? AS told before, I've managed to do it to regular columns via the columnToPropertyName, but didn't figure out how to do it to the primary keys.

Any lead would be useful. Thanks a lot!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 30, 2007 9:16 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
marcelotmelo wrote:
I have the same structure than you, all my entity classes are inherited from a base class, which has 4 base properties, one for the primary key, two for auditing and one for description.


ok.

Quote:
Anyway, I've succesfully renamed the ordinary columns for my super properties, but I could only change the name of the properties, I would like to change the method itself, making it return super.dosomething(). Is it doable via a custom reverse engineering strategy?


no, that is not the job of the reverse engineering but the job of the code generation. And thus you will need to customize the templates used to create the pojos so that they contain the super call or simply is not generated. (the last part should actually be possible by putting a <meta attribute="gen-property">false</meta> in reveng.xml or similar in a custom reveng strategy)

Quote:
Is it also possible to supress the setter methods for these properties (I need the getters because that's where the annotations go, but the setters are the same as the super.


see above.

Quote:
Also, I would like that all the represented PK's on the classes (ids) would have its property renamed to "id" (without the setters again, since they're all the same type and it's already on the super class), even though there are differente column names. You've mentioned it on your early posts, but how did you do it? AS told before, I've managed to do it to regular columns via the columnToPropertyName, but didn't figure out how to do it to the primary keys.


columnToPropertyName shoud definitly also work for primary keys. which version are you using ?

Could you show the table layout, reveng.xml and the generated result if it does not work in the latest version.

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 19 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours [ DST ]


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

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.