-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Native Hibernate (Session) with JPA Annotations
PostPosted: Wed Oct 03, 2007 12:30 pm 
Newbie

Joined: Sun Sep 11, 2005 1:16 am
Posts: 5
Can native Hibernate be used with JPA Annotations? I've marked my model classes up with JPA Annotations and am using Hibernate Session. Is this supposed to work, or do I have to use the JPA EntityManager in conjunction with annotations? I have THE book, but I cannot find a definitive statement on this.

Thanks,
Michael-


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 29, 2007 7:08 am 
Beginner
Beginner

Joined: Tue Jan 18, 2005 9:37 am
Posts: 29
Location: The Netherlands
Hi Michael,

Yes, it is supposed to work.
Have a look at hibernate annotations implementation.
This implements the javax.persistence api for annotations plus some extra things.

You might also need a HibernateUtil class.
Have a look at the examples bundled with the annotations package.

Code:
public class HibernateUtil {

   private static Logger log = Logger.getLogger(HibernateUtil.class);



   private static AnnotationConfiguration cfg;   

   private static final SessionFactory   sessionFactory;

   

   static {

      try {

         cfg = new AnnotationConfiguration();

         Class[] classes = getMappings();

         for ( int i = 0; i < classes.length ; i++ ) {

            cfg.addAnnotatedClass( classes[i] );

         }



         sessionFactory = cfg.buildSessionFactory();

      } catch (Throwable ex) {

         log.error("error initializing Hibernate sessionFactory",ex);

         throw new ExceptionInInitializerError(ex);

      }

   }

   

   public static Session getSession() throws HibernateException {

      return sessionFactory.openSession();

   }

   

   protected static Class[] getMappings() {

      return new Class[]{

            Text.class,

            Sentence.class,

      };    

   }



   

}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.