Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

java - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...