使用 JPA 更新

发布于 2024-08-21 06:40:44 字数 1747 浏览 6 评论 0原文

我正在 Web 应用程序中使用 glassfish v2 和持久性。

我在 Web 应用程序中使用普通的 java 类文件调用持久性代码,

我可以使用此代码轻松选择: -

   @PersistenceUnit
public EntityManagerFactory emf;
EntityManager em;


public List fname (String id) {
    String fname = null;
    List persons = null;
    //private PersistenceManagerFactory persistenceManagerFactory;

    try {
        emf = Persistence.createEntityManagerFactory("WebApplicationSecurityPU");

        em = emf.createEntityManager();
        persons = em.createQuery("select r from Roleuser r").getResultList();

        int i=0;
        for (i=0;i<persons.size(); i++)
            System.out.println("Testing n "+ i +" " + persons.get(i));

    } catch(Exception e) {
        System.out.println("" + e);
    }
    finally {
        if(em != null) {
            em.close();
        }
    }
    return persons;
}

我想使用 JTA 进行更新,因为 persistence.xml 文件具有 transaction-type="JTA"

当我尝试使用此代码进行更新时,我得到一个 nullPointerException,日志中没有任何痕迹

     @PersistenceUnit
public EntityManagerFactory emf;
EntityManager em;
Context context;
@Resource
private UserTransaction utx;

public List fname (String id) {

    String fname = null;
    List persons = null;


    try {
        emf = Persistence.createEntityManagerFactory("WebApplicationSecurityPU");

        utx.begin();
        em = emf.createEntityManager();

        int m = em.createQuery("update Roleuser r set r.firstName = 'Jignesh I' where r.userID=9").executeUpdate();

        utx.commit();


    } catch(Exception e) {
        System.out.println("" + e);
    }
    finally {
        if(em != null) {
            em.close();
        }
    }
    return persons;
}

任何帮助

谢谢

Pradyut

I m using glassfish v2 and persistence in a web application.

I m calling persistence codes using a normal java class file inside a web Application

I can select easily using this code: -

   @PersistenceUnit
public EntityManagerFactory emf;
EntityManager em;


public List fname (String id) {
    String fname = null;
    List persons = null;
    //private PersistenceManagerFactory persistenceManagerFactory;

    try {
        emf = Persistence.createEntityManagerFactory("WebApplicationSecurityPU");

        em = emf.createEntityManager();
        persons = em.createQuery("select r from Roleuser r").getResultList();

        int i=0;
        for (i=0;i<persons.size(); i++)
            System.out.println("Testing n "+ i +" " + persons.get(i));

    } catch(Exception e) {
        System.out.println("" + e);
    }
    finally {
        if(em != null) {
            em.close();
        }
    }
    return persons;
}

I want to update using JTA as the persistence.xml file has
transaction-type="JTA"

When i try using update using this code i get a nullPointerException without any traces in the log

     @PersistenceUnit
public EntityManagerFactory emf;
EntityManager em;
Context context;
@Resource
private UserTransaction utx;

public List fname (String id) {

    String fname = null;
    List persons = null;


    try {
        emf = Persistence.createEntityManagerFactory("WebApplicationSecurityPU");

        utx.begin();
        em = emf.createEntityManager();

        int m = em.createQuery("update Roleuser r set r.firstName = 'Jignesh I' where r.userID=9").executeUpdate();

        utx.commit();


    } catch(Exception e) {
        System.out.println("" + e);
    }
    finally {
        if(em != null) {
            em.close();
        }
    }
    return persons;
}

Any help

Thanks

Pradyut

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

抚你发端 2024-08-28 06:40:44
  1. 也许您的 bean 不受管理 - 即它不是任何上下文(spring、EJB)的一部分。你如何创建你的对象?
  2. 您确实不应该调用 createEntityManager() - 使用 @PersistenceContext 注入一个
  3. 您必须在使用 JTA 之前绝对确定您需要 JTA。
  4. 您似乎正在使用 PersistenceUnit,但随后重新分配 etm - 我建议删除两者并参见上面的 p2。

如果您根本不使用任何依赖注入,则删除所有注释,保留当前代码,然后键入:(

em.getTransaction().begin();
...
em.getTransaction().commit();

并在 persistence.xml 中定义 RESOURCE_LOCAL。您确实不需要 JTA)

  1. Perhaps your bean isn't managed - i.e. it's not part of any context (spring, EJB). How are you creating your object?
  2. You really should not call createEntityManager() - inject one using @PersistenceContext
  3. You must be absolutely sure you need JTA before using it.
  4. You seem to be using PersistenceUnit, but then re-assign the etm - I'd suggest drop both and see p2 above.

If you are not using any dependecy injection at all, then drop all the annotations, retain the current code, and type:

em.getTransaction().begin();
...
em.getTransaction().commit();

(and define RESOURCE_LOCAL in your persistence.xml. You really don't need JTA)

不可一世的女人 2024-08-28 06:40:44

好吧,代码应该没有任何噩梦......(至少对 glassfish 中的我来说)
带有

<persistence-unit name="WebApplicationSecurityPU" transaction-type="RESOURCE_LOCAL">

代码的

@PersistenceUnit
public EntityManagerFactory emf;
public EntityManager em;




public EntityManager getEm() {
    emf = Persistence.createEntityManagerFactory("WebApplicationSecurityPU");
    em = emf.createEntityManager();
    return em;
}

public List fname (String id) {

    String fname = null;
    List persons = null;


    try {
        System.out.println("test");

        em = this.getEm();


        em.getTransaction().begin();
        int m = em.createQuery("update Roleuser r set r.firstName = 'Jignesh H' where r.userID=9").executeUpdate();

        em.getTransaction().commit();


    } catch(Exception e) {
        System.out.println("" + e);
    }
    finally {
        if(em != null) {
            em.close();
        }
    }
    return persons;
}

persistence.xml欢迎任何改进...(实际上需要...)
(如何使用@PersistenceContext)

谢谢

Pradyut

well the code should be without any nightmares...(atleast for me in glassfish)
with the persistence.xml having

<persistence-unit name="WebApplicationSecurityPU" transaction-type="RESOURCE_LOCAL">

the code

@PersistenceUnit
public EntityManagerFactory emf;
public EntityManager em;




public EntityManager getEm() {
    emf = Persistence.createEntityManagerFactory("WebApplicationSecurityPU");
    em = emf.createEntityManager();
    return em;
}

public List fname (String id) {

    String fname = null;
    List persons = null;


    try {
        System.out.println("test");

        em = this.getEm();


        em.getTransaction().begin();
        int m = em.createQuery("update Roleuser r set r.firstName = 'Jignesh H' where r.userID=9").executeUpdate();

        em.getTransaction().commit();


    } catch(Exception e) {
        System.out.println("" + e);
    }
    finally {
        if(em != null) {
            em.close();
        }
    }
    return persons;
}

Any improvements are welcome...(actually needed...)
(How to go about using @PersistenceContext)

Thanks

Pradyut

自演自醉 2024-08-28 06:40:44

您的“普通”类很可能不是托管组件,即其生命周期由容器管理的类(如 Servlet、Servlet Filters、JSP 标记处理程序、JSF Managed Beans...)无法从资源注入中受益1
因此,UserTransactionEntityManagerFactory 都没有注入这里,因此出现了 NullPointerException

老实说,您应该尝试使用容器管理的 EntityManager,这将使您的代码不那么混乱。如果无法注入,请通过 JNDI 查找获取。请参阅下面的资源。

1 查看 Web Tier to Go使用 Java EE 5:查看资源注入,以很好地概述可以注入的内容以及注入位置。

资源

参考

  • JPA 1.0 规范
    • 第 5.2 节“获取实体管理器”
    • 第 5.6 节“容器管理的持久性上下文”

Your "normal" class is very likely not a managed component i.e. a class whose life cycle is managed by the container (like Servlets, Servlet Filters, JSP tag handlers, JSF Managed Beans, ...) and can't benefit from resource injection1.
So neither the UserTransaction nor the EntityManagerFactory are injected here, hence the NullPointerException.

Honestly, you should try to use a container managed EntityManager, this would make your code less messy. If you cannot get it injected, get it via a JNDI lookup. See the resource below.

1 Have a look at Web Tier to Go With Java EE 5: A Look at Resource Injection for a nice overview of what can be injected, and where.

Resources

References

  • JPA 1.0 specification
    • Section 5.2 "Obtaining an Entity Manager"
    • Section 5.6 "Container-managed Persistence Contexts"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文