First Hibernate project where to place addAnnotatedClass()

戏子无情 提交于 2020-01-05 04:34:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!