简体   繁体   English

Java Hibernate(JPA)查询

[英]Java Hibernate(JPA) queries

what is the right way to make a query with JPA. 用JPA进行查询的正确方法是什么? Here is my way now: 这是我现在的方式:

EntityManagerFactory factory = Persistence.createEntityManagerFactory("hellojpa");
        EntityManager em = factory.createEntityManager();
        em.getTransaction().begin();

        //query here

        em.getTransaction().commit();
        em.close();

I just started learning it and I am pretty sure that there will be more intelligent way. 我刚刚开始学习它,我很确定会有更明智的方法。 Some helper class, maybe? 也许是一些帮手课?

Thanks. 谢谢。

In my opinion it is the right way, if you use local transactions. 我认为,如果您使用本地交易,这是正确的方法。 Keep in mind that EntityManager is not thread safe. 请记住,EntityManager不是线程安全的。

You will have less boilerplate code in a JavaEE environment, where the application server takes care of EntityManager injection and uses JTA. 在应用程序服务器负责EntityManager注入并使用JTA的JavaEE环境中,您将获得更少的样板代码。

@PersistenceContect
EntityManager em;

public void foo() {
// query
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM