Does one necessarily have to make a strict choice between using annotations and hibernate configuration files, or is it possible to use both ways at the same time in the same project?
We have a large project done with Hibernate 3.2.5 and POJOs configured with .hbm.xml
files. The project uses Hibernate SessionManager
Factory and not JTA-based EntityManager
factory.
There are over a hundred POJOs configured that way and the original developer team is reluctant to switch to annotations.
Meanwhile we need to extend the project with more POJOs which we are willing to configure with Hibernate annotations and want to stay with Hibernate - if possible - and not switch to JPA.
Is it possible to use a combination of .hbm.xml
config files for some POJOs and annotations for others at the same time?
Basic entity mapping You just add an @Entity annotation to the class and an @Id annotation to the primary key attribute. Hibernate maps the entity to a database table with the same name and uses a default mapping for each attribute. You can define the same configuration with the following XML file.
hbm. xml. The mapping document is an XML document having <hibernate-mapping> as the root element, which contains all the <class> elements. The <class> elements are used to define specific mappings from a Java classes to the database tables.
In hibernate we can map a model object into a relation/table with the help of @Entity annotation.
Yes you can. The Documentation says
Note that you can mix the legacy hbm.xml use and the annotation approach. The resource element can be either an hbm file or an EJB3 XML deployment descriptor.
There are a few check point though:
You can however not declare a class several times (whether annotated or through hbm.xml). You cannot mix configuration strategies (hbm vs annotations) in an entity hierarchy either.
You can find some more information here.
Manu has given the answer but if you are going to switch to annotations and you want to convert those hbm.xml to annotations. Please go on. You can use the tools that will generate the pojo classes with annotations from existing tables. Even using those tools, You dont have to change the other code just generate the pojos like before. i.e. dont change property names. I have worked on same kinda project and I used Ant script for that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With