-->
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.  [ 12 posts ] 
Author Message
 Post subject: Generate Validation Annotations
PostPosted: Tue Sep 05, 2006 7:16 am 
Regular
Regular

Joined: Sat May 20, 2006 3:49 am
Posts: 78
Hello,

I have a question: I would like to use the hibernate validator, its really a nice thing to make validation within the POJOs. My question is, is it possible to generate the validator annotations with the hibernate.reveng.xml file?

I would like to generate the following method with validation annotations:
Code:
    @Length(min=5,max=10)
    @Column(name = "CODE_ID", unique = true, nullable = false, insertable = true, updatable = true)
    public String getCodeId() {
        return this.codeId;
    }


Regards,

Christoph[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 06, 2006 4:39 am 
Beginner
Beginner

Joined: Thu Nov 25, 2004 7:07 am
Posts: 43
Location: Germany
I want to do the same thing and I am not sure if it's part of the ReverseEngineeringStrategy or of the customer Exporter. Maybe someone can give a hint how to do this, if it's possible.

_________________
You'll never get a second chance to make a first impression!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 16, 2006 3:57 am 
Regular
Regular

Joined: Sat May 20, 2006 3:49 am
Posts: 78
caterham wrote:
I want to do the same thing and I am not sure if it's part of the ReverseEngineeringStrategy or of the customer Exporter. Maybe someone can give a hint how to do this, if it's possible.


Hello Caterham,

Perhaps we could talk about the problem, perhaps we could find a solution for us. I am from austria, my mail address is maitscha at gmx.at.

Regards.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 16, 2006 4:29 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
why don't you just discuss it here ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 16, 2006 4:33 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
...and yes, it is an exporter issue not reverse engineering; at least it is the exporter templates that should be changed to insert the validation annotations.

if the reverse engineering comes into play its because you want some additional information "injected" into the meta model.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 16, 2006 5:26 am 
Regular
Regular

Joined: Sat May 20, 2006 3:49 am
Posts: 78
max wrote:
...and yes, it is an exporter issue not reverse engineering; at least it is the exporter templates that should be changed to insert the validation annotations.


Ok, I have looked into the documentation of the exporter. I have to change some of the .ftl default templates, I think the Ejb3PropertyGetAnnotation.ftl, put it into the classpath and run the hibernate-tools with the modified one, right?

Perhaps it is also necessary to override one of the methods, I think pojo.generateBasicAnnotation(property) is a good place to put my custom validator annotations.

Is it the right way?

Regards.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 16, 2006 5:28 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
i would generate them as something seperate - non dependent on the jpa/ejb3 annotations.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 16, 2006 7:49 am 
Regular
Regular

Joined: Sat May 20, 2006 3:49 am
Posts: 78
Hello Caterham,

So far, I have found a solution for my problem. As max has described, the annotations could be added with some modifications in the export templates. I have extracted the file Ejb3PropertyGetAnnotation.ftl from the sources and added my own script for adding the annotations:

Ejb3PropertyGetAnnotation.ftl:
Code:
${pojo.generateBasicAnnotation(property)}
${pojo.generateAnnColumnAnnotation(property)}
<#include "PropertyValidations.ftl"/>


PropertyValidations.ftl:
Code:
<#switch pojo.getShortName()>
  <#case "Parameter">
    <#switch property.getName()>
       <#case "id">     @Length(min=3, max=10) <#break>
       <#case "value">  @Length(min=1, max=99) <#break>
    </#switch>
  <#break>
</#switch>


Then add the directory ??/pojo/ with the ftl-files to the template directory in Hibernate Tools and generate the POJOs.

Fine!

Regards


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 16, 2006 8:55 am 
Beginner
Beginner

Joined: Thu Nov 25, 2004 7:07 am
Posts: 43
Location: Germany
I also found a very generic solution for the problem. I can tell you on monday via e-mail. if max is interessted i can contribute my new validation template.

_________________
You'll never get a second chance to make a first impression!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 16, 2006 11:56 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
what's the "very generic solution" ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 17, 2006 4:46 am 
Regular
Regular

Joined: Sat May 20, 2006 3:49 am
Posts: 78
caterham wrote:
I also found a very generic solution for the problem. I can tell you on monday via e-mail. if max is interessted i can contribute my new validation template.

yes, please let us know your solution, i am very interested in it.


Top
 Profile  
 
 Post subject: Hibernate validator annotations from reverse engineering
PostPosted: Mon Dec 11, 2006 7:46 pm 
Newbie

Joined: Tue Nov 28, 2006 8:08 pm
Posts: 1
This code generates the hibernate validator annotations out of the FTL

The pattern annotations are very generic and tied to just a basic regex. (commented below but you can uncomment and change to suit your needs)

Most of this is a duplication of the Java code in FTL


In Ejb3PropertyGetAnnotation.ftl

add these lines before ${pojo.generateAnnColumnAnnotation(property)}

---xxx---------end cut here------xxx------

// hibernate validation annotations
<#foreach dummy in property.getColumnIterator()>
<#if dummy.isNullable()>
<#assign myMin = 0/>
<#else>
<#assign myMin = 1/>
@NotNull(message="{validator.notNull}")
</#if>
<#if dummy.getLength() != 255 && dummy.getLength() != 7>
<#assign myMax = dummy.getLength()?string.number/>
<#assign myMax = myMax?replace(",","")/>
<#if pojo.getJavaTypeName(property, jdk5) != "char">
@Length(min=${myMin}, max=${myMax}, message="{validator.length}")
</#if>
</#if>
<#if dummy.getPrecision() != 19>
// @Pattern(regex="\\d{${myMin},${dummy.getPrecision()}}", message="{validator.pattern}")
<#else>
<#assign myMax = dummy.getLength()?string.number/>
<#assign myMax = myMax?replace(",","")/>
<#if dummy.getLength() != 255 && dummy.getLength() != 7>
// @Pattern(regex="\\S{${myMin},${myMax}}", message="{validator.pattern}")
</#if>
</#if>
</#foreach>
---xxx---------end cut here------xxxxx


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 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.