Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: portable @GeneratedValue + @GenericGenerator annotations
PostPosted: Fri Feb 12, 2010 6:56 am 
Newbie

Joined: Fri Feb 12, 2010 6:33 am
Posts: 3
Hi all!

I'm using hibernate-tools to generate java code from an HSQLDB SQL script. Even when the documentation is not very detailed regarding the reverse engineering process, I've been able to do everything that I've needed until now (I'm even supporting inheritance using joined tables), since the source code is pretty understandable. Anyway, today I ran into a problem that, looking into the source code, doesn't seem solvable to me.

Before starting, please correct me if I'm assuming something wrong or if I have a misunderstanding of something.

One of my requisites of my application is that the generated pojos are suitable either for Oracle and HSQLDB. This is working fine, but I wanted to improve the ID generation, since right now I'm using just a plain @GeneratedValue annotation, which is the same that using <generator class="native" /> in a mapping file. This translates to use identity() in HSQLDB, and the default hibernate_sequence in Oracle. My idea was to use one different generator per table in Oracle, and maintain the usage of identity in HSQLDB.

In the mapping file this is trivial. <generator class="native"><param name="sequence">table_sequence</param></generator> will maintain Hibernate choosing between using identity or sequence depending on the underlying DB, and if sequence is the case, then it will use the table_sequence. If using annotations, I'd have to use @GenericGenerator, with strategy "native", and the same parameter "sequence" with value "table_sequence", and in @GeneratedValue use this generic generator. I've tested this by hand, and is working properly, both with Oracle and HSQLDB, so I think that is what I want to have generated in my pojos.

Now comes the tricky thing. I need a native strategy. I also need to set a sequence name for the case that a sequence is used by hibernate. The current code on hibernate-tools (org.hibernate.tool.EntityPOJOClass#generateAnnIdGenerator) does not allow to have both things specified at the same time. Either you set your strategy to "native", which generates a plain @GeneratedValue annotation, or set your strategy to something different from "assigned", "native", "identity", "sequence" or "org.hibernate.id.MultipleHiLoPerTableGenerator" to get a @GenericGenerator annotation where you can put your parameters. This makes me impossible to generate portable pojos.

I don't know if it is a known issue, but I wasn't able to find any reference to this in the forums. Also looking into the source code I don't see a solution.

Thanks for reading and (hopefully) answering :)

Cheers!

Rodrigo


Top
 Profile  
 
 Post subject: Re: portable @GeneratedValue + @GenericGenerator annotations
PostPosted: Fri Feb 12, 2010 8:24 am 
Newbie

Joined: Fri Feb 12, 2010 6:33 am
Posts: 3
BTW, I've been able to get a solution by overriding the Ejb3PropertyGetAnnotation template, and creating a custom MetaAttribute for my class. I check if it is, then I write the annotations that I need. Anyways, it would be nice to don't have to do this, but use the standard mechanisms provided by hibernate-tools

Cheers!

Rodrigo


Top
 Profile  
 
 Post subject: Re: portable @GeneratedValue + @GenericGenerator annotations
PostPosted: Mon Feb 15, 2010 5:12 am 
Senior
Senior

Joined: Tue Aug 04, 2009 7:45 am
Posts: 124
What about using custom templates?
Alter Ejb3PropertyGetAnnotation.ftl to produce your code instead of calling pojo#generateAnnIdGenerator().


Top
 Profile  
 
 Post subject: Re: portable @GeneratedValue + @GenericGenerator annotations
PostPosted: Mon Feb 15, 2010 10:50 am 
Newbie

Joined: Fri Feb 12, 2010 6:33 am
Posts: 3
Dmitry Geraskov wrote:
What about using custom templates?
Alter Ejb3PropertyGetAnnotation.ftl to produce your code instead of calling pojo#generateAnnIdGenerator().


Yes, that's exactly what I did (see my previous post). Anyway, it would be nice to have this feature integrated in the hibernate-tools: I always leave the template overwriting as the last option.

Cheers!

Rodrigo


Top
 Profile  
 
 Post subject: Re: portable @GeneratedValue + @GenericGenerator annotations
PostPosted: Wed Nov 30, 2011 1:09 pm 
Senior
Senior

Joined: Fri May 08, 2009 12:27 pm
Posts: 168
How are you supporting inheritance?
I see no way to do it, and just hit a snag because I need it.


Top
 Profile  
 
 Post subject: Re: portable @GeneratedValue + @GenericGenerator annotations
PostPosted: Wed Dec 14, 2011 11:46 am 
Newbie

Joined: Thu Dec 30, 2010 12:10 pm
Posts: 9
Location: France
Hello,

I have the same problem as you. I would like to use a GenericGenerator by default. And the ability to override it if necessary in the reveng.xml.

I found the solution to override Ejb3PropertyGetAnnotation template :

Code:
<#if ejb3>
<#if pojo.hasIdentifierProperty()>
<#if property.equals(clazz.identifierProperty)>
<#if clazz.identifier.identifierGeneratorStrategy != "assigned" >
${pojo.generateAnnIdGenerator()}
<#else>
  <#if c2j.isComponent(property) >
  @${pojo.importType("javax.persistence.EmbeddedId")}
  <#else> 
  @${pojo.importType("org.hibernate.annotations.GenericGenerator")}(name = "generator", strategy = "increment")
  @${pojo.importType("javax.persistence.GeneratedValue")}(generator = "generator")
  @${pojo.importType("javax.persistence.Id")}
  </#if>
</#if>
<#-- if this is the id property (getter)-->
<#-- explicitly set the column name for this property-->
</#if>
</#if>

<#if c2h.isOneToOne(property)>
${pojo.generateOneToOneAnnotation(property, cfg)}
<#elseif c2h.isManyToOne(property)>
${pojo.generateManyToOneAnnotation(property)}
<#--TODO support optional and targetEntity-->   
${pojo.generateJoinColumnsAnnotation(property, cfg)}
<#elseif c2h.isCollection(property)>
${pojo.generateCollectionAnnotation(property, cfg)}
<#else>
${pojo.generateBasicAnnotation(property)}
${pojo.generateAnnColumnAnnotation(property)}
</#if>
</#if>


References :
http://docs.jboss.org/jbosside/hibernate-tools/#gen14
http://docs.jboss.org/tools/2.1.0.Beta1 ... ml_single/
viewtopic.php?p=2320884
http://anonsvn.jboss.org/repos/hibernat ... Class.java
http://anonsvn.jboss.org/repos/hibernat ... aTool.java
http://anonsvn.jboss.org/repos/hibernat ... ategy.java
http://simplebpm.googlecode.com/svn-his ... asedao.ftl


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

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.