JPA和JSF:注入EntityManager的正确方法 [英] JPA and JSF: right way of injecting EntityManager

查看:203
本文介绍了JPA和JSF:注入EntityManager的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理几个小时,但仍然没有弄清楚在JSF中使用JPA的正确方式。



我有一个会话范围的托管bean,可以做很多事情,其中​​一个是持久化一些实体对象。
一开始,我创建了一个生成器方法,请求作用域,它构建我的EntityManager对象。无论如何,在这里读取一个将请求的范围bean注入到会话范围的bean中,将该实例包装到Instance对象中。完成。但是当我尝试使用EM时,我得到了一个事务所需的异常。继续阅读有人告诉我必须使用PersistenceContext注释将EM直接注入到托管的bean中。无论如何,我认为使用persistenceUnit注解并在需要它们的方法中创建我的em有更多的意义。注入工作,但我仍然得到一个事务所需的异常,当我尝试坚持的东西!



所以在这里我问:什么是这样做的正确方法? p>

编辑:我正在使用默认设置的Jboss eap 6.2。我在WEB-INF内部使用xml文件部署我的数据源,所以我使用JTA。

解决方案

一个白痴。我已经在过去发生过这个问题,但我忘了。



对于每个人来说,在这里解决同样的问题就是解决方案。
容器管理的事务仅在容器是EJB时才起作用。如果容器是JSF,它将不起作用!注入成功但创建的对象无法正常工作并不重要。



解决这个问题的方法是创建一个EJB DAO对象为你处理数据库。就像

  @Named 
@Stateless
public class MyDAO {

@PersistenceContext(unitName =SRA)
private EntityManager em;

public void save(Object o){
em.persist(o);
em.flush();
}

}

然后在你的托管bean内部注入这些对象:

pre $ @ManagedBean
@SessionScoped
public class MyManagedBean {
@EJB
私人MyDAO dao;

public void action(){
....
dao.save(o);
}

}

直接将一个EntityManager注入到一个Managed Bean中会给你一个Transaction Required Exception。



我仍然不确定如何将这样的东西注入到一个更广泛的bean中。我会稍后再研究。


It has been some hours I'm working on this but still I haven't figured out what is the right way of using JPA with JSF.

I have a session scoped managed bean that can do a lot of things, and one of them is persisting some entity objects. At the beginning, I created a producer method, request scoped, that build my EntityManager objects. Anyway a read here that injecting a requested scoped bean into a session scoped ones is done wrapping that instance into an Instance object. Done. But when I try to use that EM I got a transaction required exception. Keep on reading on the internet someone tells that I have to inject the EM directly into my managed beans using the PersistenceContext annotation. Anyway I thought it has more sense using a persistenceUnit annotation and creating my em inside my methods where I needed them. Injection works, but I still get a transaction required exception when I try to persist something!

So here I am asking: what is the right way of doing this?

edit: I'm using Jboss eap 6.2 using default settings. I'm deploying my data source using a xml file inside WEB-INF, so I'm using JTA.

解决方案

I'm an idiot. I've already clashed with this problem in the past but I forgot.

To everyone that steps here with the same problem here is the solution. Container Managed Transactions only works if the container is an EJB. It does NOT works if the container is JSF! It doesn't matters how you inject your entity managers, injections succeed but the created object will not works.

The way of solving it, is creating an EJB DAO object that deal with the database for you. Something like

@Named
@Stateless
public class MyDAO {

@PersistenceContext(unitName = "SRA")
private EntityManager em;

public void save(Object o) {
    em.persist(o);
    em.flush();
}

}

and then injecting those object inside your managed bean like this:

@ManagedBean
@SessionScoped
public class MyManagedBean {
@EJB
private MyDAO dao;

public void action() {
....
    dao.save(o);
}

}

injecting directly an EntityManager into a Managed Bean will give you a Transaction Required Exception.

I'm still unsure about injecting something like that into a broader scoped bean. I will look into this later.

这篇关于JPA和JSF:注入EntityManager的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆