JPA and JSF: right way of injecting EntityManager

fer.marino

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.

fer.marino

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JPA EJB PersistenceContext not injecting EntityManager

From Dev

Spring JPA/Hibernate EmptyInterceptor not injecting Entitymanager/Spring beans

From Dev

Injecting EntityManager in JBoss/WildFly

From Dev

Injecting EntityManager with Guice

From Dev

Injecting EntityManager using Spring ( Null Pointer Exception )

From Dev

Injecting EntityManager-dependend service into Listener

From Dev

Injecting EntityManager in servlet, it seems not thread safe

From Dev

Injecting EntityManager using Spring ( Null Pointer Exception )

From Dev

Injecting EntityManager-dependend service into Listener

From Dev

JPA EntityManager and character encoding

From Dev

Is there a stateless version of the JPA EntityManager?

From Dev

JPA EntityManager Construction

From Dev

JPA EntityManager Construction

From Dev

Is there a stateless version of the JPA EntityManager?

From Dev

JPA Entity not registered with EntityManager

From Dev

JPA EntityManager and character encoding

From Dev

EntityManager merge() in JPA

From Dev

JPA - equivalence of findByProperty() of EntityManager

From Dev

Spring Boot + JPA inject EntityManager no EntityManager available

From Dev

Injecting a CDI SessionScoped bean into a JSF ValueChangeListener

From Dev

Best way to close an entityManager

From Dev

JPA + Hibernate = No Persistence provider for EntityManager

From Dev

No persistence provider for EntityManager, JPA configuration

From Dev

Regarding Thread safety and JPA EntityManager

From Dev

java - JPA EntityManager Injection fails

From Dev

JPA EntityManager find entity by ID

From Dev

JPA EntityManager find entity by ID

From Dev

JPA EntityManager - when the transaction starts?

From Dev

Entitymanager is null spring JPA configuration

Related Related

  1. 1

    JPA EJB PersistenceContext not injecting EntityManager

  2. 2

    Spring JPA/Hibernate EmptyInterceptor not injecting Entitymanager/Spring beans

  3. 3

    Injecting EntityManager in JBoss/WildFly

  4. 4

    Injecting EntityManager with Guice

  5. 5

    Injecting EntityManager using Spring ( Null Pointer Exception )

  6. 6

    Injecting EntityManager-dependend service into Listener

  7. 7

    Injecting EntityManager in servlet, it seems not thread safe

  8. 8

    Injecting EntityManager using Spring ( Null Pointer Exception )

  9. 9

    Injecting EntityManager-dependend service into Listener

  10. 10

    JPA EntityManager and character encoding

  11. 11

    Is there a stateless version of the JPA EntityManager?

  12. 12

    JPA EntityManager Construction

  13. 13

    JPA EntityManager Construction

  14. 14

    Is there a stateless version of the JPA EntityManager?

  15. 15

    JPA Entity not registered with EntityManager

  16. 16

    JPA EntityManager and character encoding

  17. 17

    EntityManager merge() in JPA

  18. 18

    JPA - equivalence of findByProperty() of EntityManager

  19. 19

    Spring Boot + JPA inject EntityManager no EntityManager available

  20. 20

    Injecting a CDI SessionScoped bean into a JSF ValueChangeListener

  21. 21

    Best way to close an entityManager

  22. 22

    JPA + Hibernate = No Persistence provider for EntityManager

  23. 23

    No persistence provider for EntityManager, JPA configuration

  24. 24

    Regarding Thread safety and JPA EntityManager

  25. 25

    java - JPA EntityManager Injection fails

  26. 26

    JPA EntityManager find entity by ID

  27. 27

    JPA EntityManager find entity by ID

  28. 28

    JPA EntityManager - when the transaction starts?

  29. 29

    Entitymanager is null spring JPA configuration

HotTag

Archive