-->
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.  [ 11 posts ] 
Author Message
 Post subject: Generate annotations from mapping?
PostPosted: Mon Dec 12, 2005 8:36 pm 
Beginner
Beginner

Joined: Fri Nov 04, 2005 3:51 pm
Posts: 32
Hibernate version:3.1

Maybe this question belongs in the tools forum, but I kind of don't know on the difference between hbm2java, pojoexporter and which ones are packaged in hibernate core vs hibernate tools.
Anyway, is it possible to generate annotations such as @NotNull in my POJOs using one of the code generation tools?
If so, could someone give me and example or point me to the relevant doc?

Currently I am using the hbm2java in hibernate.jar through ant task. It works very well, but I don't see this option.

Thank You.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 1:50 am 
Beginner
Beginner

Joined: Fri Nov 04, 2005 3:51 pm
Posts: 32
It is possible to generate annotations. I installed Hibernate tools in eclipse and found it there (Run -> Hibernate Code Generation). From forum, it looks like you can create your own templates for annotation creation, and run from Ant task, etc.
Everybody in forum seem to know exactly how to do these things except me. Am I missing the documentation or is everybody just smarter than I am to figure it out?

Side note on Hibernate tools: If I specify my mapping in my .cfg.xml file, Hibernate tools says it can't find the mapping file and stops execution. I have to comment it out of the config file and manually specify it.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 2:45 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
thats because you need to put the build path into the console configuration classpath (as it says in the docs ;)

And yes we have ejb3 entity pojo generation in the tools (both via ant and eclipse) you just need to select it in the ui or set ejb3="true" in ant.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 3:28 am 
Beginner
Beginner

Joined: Fri Nov 04, 2005 3:51 pm
Posts: 32
Ok. Mange tak. I'm still wondering if there is additional documenation that I am missing ;)
I see this ejb3="true" generates annotations relevant to schema generation. What I am really after is annotations for use with Hibernate Validator as described here http://www.hibernate.org/hib_docs/annotations/reference/en/html/validator.html.

My fault, I wasn't specific because I'm a little confused by the whole thing as I'm new to it. So I'll try to be specific as I can.

I have a mapping.hbm.xml. Suppose I add a bunch of <meta/> elements that describe validation constraints (not nullable, min, max, etc). During pojo generation, i want these to turn into Hibernate Validator annotations.

If this functionality is avaialble out-of-the-box:
Is it documented?
If so, please tell me where... maybe I'm blind!

If this functionality is not available out of the box:
Is using a custom velocity template the answer?
I see in forums that people modify these, but I can't even find one to modify or know where to begin. I wouldn't even know how to specify a custom one in the ant task (again, I'm blind, I don't see the doc). And I don't know where to find the default one to look at it (I did file search on .vm and came up with nothing).

Sorry for all the newb questions. If this is all explained somewhere, please, someone show me the way! If not, I'd be very grateful for any small or large hints.

[/url]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 3:59 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Selv tak.

You are on the track about using custom templates since we don't provide validation out of the box.

the default templates are in hibernate-tools.jar which is in org.hibernate.eclipse/lib/tools (from the docs: "The hibernate-tools.jar is located in your eclipse plugins directory at /plugins/org.hibernate.eclipse.x.x.x/lib/tools/hibernate-tools.jar")

And how you do things is (again from the docs):

"2.3 Velocity-Templates
You can customize the generated files easily with velocity templates. For a detailed description see: http://jakarta.apache.org/velocity/docs/user-guide.html. The default template for example the pojo-generation can be found in hibernatetools.jar in pojo/javaclass.vm. To specify you own template you can use something like:


<hbm2java templatespath="${mypath}"/>

and put javaclass.vm in the directory ${mypath}/pojo."

There is one thing wrong here though - the template is pojo/Pojo.vm, but just look in that directory and you should be able to see how the things are structured.

And this is from the doc placed on tools.hibernate.org (click the Documentation)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 4:12 am 
Beginner
Beginner

Joined: Fri Nov 04, 2005 3:51 pm
Posts: 32
Wow, I really was blind. I had not found this document http://www.hibernate.org/hib_docs/tools/ant/index.html. I was only looking at one small html file in the distribution. I think between the info you gave me and the doc, I should be able to get there.
Thanks for your patience & help!


Top
 Profile  
 
 Post subject: Re: Generate annotations from mapping?
PostPosted: Wed Dec 21, 2005 7:14 pm 
Beginner
Beginner

Joined: Wed Oct 19, 2005 4:11 am
Posts: 48
franknelsen wrote:
Anyway, is it possible to generate annotations such as @NotNull in my POJOs ...


I would have thought that "adding" stuff, like annotations to POJOs would make them non POJO anymore. They really aren't plain old Java files if they have extra stuff that's not part of the Java language annotated to them.

I don't know that the general consensus is on this but didn't Hibernate 'used to' suggest mappings be embedded into the Java files but this was seen as 'dirty' and so then we migrated to a cleaner solution that separated the mapping metadata from the POJO files by placing them in separate .hbm.xml files. Isn't the use of annotations just 'dirtying' the POJO source files once more and going back to the past?

For me I like keeping my POJO's POJO! I'd like to know other people's thought's on this.

_________________
Chris Colman
http://stepaheadsoftware.com/products/j ... avelin.htm
Javelin: Lightweight, non intrusive, POJO, Java modeling & coding with automatic Hibernate and JDO meta data generation.
Don't forget to credit ;)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 3:00 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Hi chrisco,

If you prefer using mapping files instead of annotations then use mapping files instead of annotations.

And annotations might have the "price" of having more "stuff" into the pojo's but that that gives you benefits (one thing in one place) without making them dependent on anything at runtime - you can still use these classes as any other POJO (which is the whole point of the POJO model)

Remember you don't even need the annotations classes in the runtime classpath when using the classes as POJO's.

/max

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 3:32 am 
Beginner
Beginner

Joined: Wed Oct 19, 2005 4:11 am
Posts: 48
max wrote:
And annotations might have the "price" of having more "stuff" into the pojo's but that that gives you benefits (one thing in one place)


It's 'kind of' more like two things in one place: Java class definition and O/R mapping metadata in one place.

Quote:
without making them dependent on anything at runtime - you can still use these classes as any other POJO (which is the whole point of the POJO model)
Remember you don't even need the annotations classes in the runtime classpath when using the classes as POJO's.


This makes me wonder how the metadata was supplied to the runtime when embedded the old fashioned way: as a comment embedded in the source. Did the source files have to be shipped with the run time so that the meta data could be parsed from them at run time?

_________________
Chris Colman
http://stepaheadsoftware.com/products/j ... avelin.htm
Javelin: Lightweight, non intrusive, POJO, Java modeling & coding with automatic Hibernate and JDO meta data generation.
Don't forget to credit ;)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 3:40 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Yes - the related things in one place. Some prefer that, some don't that's life and that's how it will continue to be. Use what makes sense for you and your development process.

"as a comment embedded in the source" ? You are talking about xdoclet mappings ? Here you had a buildtime step to generate the mapping files which then were used.

/max

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 22, 2005 3:49 am 
Beginner
Beginner

Joined: Wed Oct 19, 2005 4:11 am
Posts: 48
max wrote:
"as a comment embedded in the source" ? You are talking about xdoclet mappings ? Here you had a buildtime step to generate the mapping files which then were used.

Arh ok, that's how they did it. Cool, you've just helped to fill in a historical gap in the Hibernate section of the neural network in my head. I'd heard of that method but never used it.

_________________
Chris Colman
http://stepaheadsoftware.com/products/j ... avelin.htm
Javelin: Lightweight, non intrusive, POJO, Java modeling & coding with automatic Hibernate and JDO meta data generation.
Don't forget to credit ;)


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