2.2. Configuration
First, set up your classpath (after you have created a new project in your favorite IDE):
- Copy all Hibernate3 core and required 3rd party library files (see lib/README.txt in Hibernate).
- Copy
hibernate-annotations.jar
,lib/hibernate-comons-annotations.jar
andlib/ejb3-persistence.jar
from the Hibernate Annotations distribution to your classpath as well.
We also recommend a small wrapper class to startup Hibernate in a static initializer block, known as
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
HibernateUtil
. You might have seen this class in various forms in other areas of the Hibernate documentation. For Annotation support you have to enhance this helper class as follows:
Interesting here is the use of
AnnotationConfiguration
. The packages and annotated classes are declared in your regular XML configuration file (usually hibernate.cfg.xml
). Here is the equivalent of the above declaration:
Note that you can mix the hbm.xml use and the new annotation one. The resource element can be either an hbm file or an EJB3 XML deployment descriptor. The distinction is transparent for your configuration process.
Alternatively, you can define the annotated classes and packages using the programmatic API
sessionFactory = new AnnotationConfiguration() .addPackage("test.animals") //the fully qualified package name .addAnnotatedClass(Flight.class) .addAnnotatedClass(Sky.class) .addAnnotatedClass(Person.class) .addAnnotatedClass(Dog.class) .addResource("test/animals/orm.xml") configure()..buildSessionFactory();
sessionFactory = new AnnotationConfiguration() .addPackage("test.animals") //the fully qualified package name .addAnnotatedClass(Flight.class) .addAnnotatedClass(Sky.class) .addAnnotatedClass(Person.class) .addAnnotatedClass(Dog.class)
.addResource("test/animals/orm.xml")
configure()..buildSessionFactory();
You can also use the Hibernate EntityManager which has its own configuration mechanism. Please refer to this project documentation for more details.
There is no other difference in the way you use Hibernate APIs with annotations, except for this startup routine change or in the configuration file. You can use your favorite configuration method for other properties (
hibernate.properties
, hibernate.cfg.xml
, programmatic APIs, etc). You can even mix annotated persistent classes and classic hbm.cfg.xml
declarations with the same SessionFactory
. You can however not declare a class several times (whether annotated or through hbm.xml). You cannot mix configuration strategies (hbm vs annotations) in a mapped entity hierarchy either.
To ease the migration process from hbm files to annotations, the configuration mechanism detects the mapping duplication between annotations and hbm files. HBM files are then prioritized over annotated metadata on a class to class basis. You can change the priority using
hibernate.mapping.precedence
property. The default is hbm, class
, changing it to class, hbm
will prioritize the annotated classes over hbm files when a conflict occurs.