Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use both annotations and hbm.xml files in the same project in Hibernate?

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?

like image 265
Skyhan Avatar asked Nov 29 '11 13:11

Skyhan


People also ask

How to Map entity with hibernate?

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.

What is the use of HBM file in hibernate?

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.

Which annotation is used for mapping is a relationship in hibernate?

In hibernate we can map a model object into a relation/table with the help of @Entity annotation.


2 Answers

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.

like image 58
ManuPK Avatar answered Sep 21 '22 00:09

ManuPK


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.

like image 33
Nandkumar Tekale Avatar answered Sep 20 '22 00:09

Nandkumar Tekale