cornercorner
FeaturesPluginsDocs & SupportCommunityPartners

HiberanteAnnotations

Revision as of 18:16, 6 November 2009 by Admin (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)

Hibernate Annotations Support for 6.5

This document gives a brief description about how Hibernate Annotations can be supported in Netbeans IDE.

  • Hibernate Annotations are to replace Hibernate Mapping XML files with inline meta data.
  • Hibernate Annotations support auto completion as we type in the IDE and they are type safe.
  • In order to support Hibernate Annotations,
    1. hibernate-annotations.jar
    2. hibernate- commons-annotations.jar
    3. ejb3-persistence.jar (in the case of J2SE projects) are required to be placed in the project class path.
  • Hibernate reverse engineering feature generates POJOs with annotations. During the generation,
    1. If the user picks both HBM, POJOs with EJB3, then the configuration file should be updated
 with the hbm files. 
    1. If the user checks only the POJOs with EJB3 annotations without HBM xml files, then the
 configuration file should be updated with the annotated classes.

Eg:


<hibernate-configuration>
<session-factory>
.....
<mapping class="travel.Person"/>
<mapping class="travel.Trip"/>
</session-factory>
</hibernate-configuration>

  • When the user creates HibernateUtil, an instance of sessionFactory should be taken from AnnotationConfiguration instead of Configuration object.

public class HibernateUtil {
    private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml) 
            // config file.
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

It is also possible to add the annotated classes using the programmatic API.

Eg:


 sessionFactory = new AnnotationConfiguration()
                    .addPackage("travel") //the fully qualified package name
                    .addAnnotatedClass(travel.Person.class)
                    .addAnnotatedClass(travel.Trip.class)
                    configure()..buildSessionFactory();


Since AnnotationConfiguration is a subclass of Configuration, the HibernateUtil java class template needs to be updated to use AnnotationConfiguration.