Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading multiple annotated classes in Hibernate

I am using hibernate annotations and for that in the hibernate.cfg.xml i need to add annotated classes like this <mapping class="p.Customer" /> here p is the package name and Customer is the annotated bean.

Suppose i have 20 such kind of annotated classes, it means I have to write 20 mapping lines for that class.In Spring there is a property packageToScan that can be used to register/load all hibernate annotated classes in the specified package.

Since I am not using Spring, can we have the same functionality in Hibernate?

Also I found one tag in hibernate.cfg.xml <mapping package="" /> at first I thought this will do the job for me, but it didn't work. I didn't get what is the use of this property.

like image 308
Anand Avatar asked Nov 12 '22 22:11

Anand


1 Answers

The magic of annotations parsing is done when creating session factory. Hibernate can do it without spring. Spring actually just wraps the hibernate functionality.

Please take a look on this article: http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-overview

They show how to register classes in session factory, so the annotations are used. They indeed do not provide functionality of scanning but you can either implement it yourself or better use other package. I used "reflections" package for similar purpose. I mean I scanned my classpath to locate classes according to my criteria using reflections package. I did not use it for hibernate but I am sure it is possible.

Here is the reference that can help you. http://code.google.com/p/reflections/

like image 176
AlexR Avatar answered Nov 15 '22 12:11

AlexR