问题
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
回答1:
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).
回答2:
Partenon is right, you should bootstrap JPA with a persistence.xml.
The Stripes web framework it self does not offer any persistence services. But to make life easier there is an Stripersist extension that offers an out of the box session in view pattern (starts a transaction before the actionbean and does a roll back after the request is handled). Very good examples of how to use and configure Stripersist can be found in the book: Stripes: ...and Java web development is fun again
来源:https://stackoverflow.com/questions/5731203/first-hibernate-project-where-to-place-addannotatedclass