Search code examples
databasehibernateservletsstripes

First Hibernate project where to place addAnnotatedClass()


Hello all I'm trying to build up my first Hibernate project for a Web app, but i'm having some issues Trying to find out where to place the method:

AnnotationConfiguration config = 
            new AnnotationConfiguration();

config.addAnnotatedClass(Object.class);

config.configure();

i have some java beans decorated with annotations, shel i just insert it in the same class there the bean is?

Thank you


Solution

  • Ideally, you'd call this only if you are developing a standalone application. In a Java EE environment, you'd just define a persistence.xml file (or hibernate.cfg.xml) in your deployment archive and the container (like JBoss AS) would make a @PersistenceContext (EntityManager) available to you.

    In a standalone application, you'd call this in your "Bootstrap" code. The one which sets up the environment.

    In "non-Java EE" web applications (seriously, who still uses that?), you'd have to resort to some "hacks", like doing some initialization during context startup (so that you won't need to run this for all requests, as it's an expensive operation).