JTA & MySQL - How to Retrieve records from databas

2019-07-11 07:30发布

I am new to JTA and I need a method to retrieve some some elements from the database. I can do this through EntityManager but that works only for ResourceLocal. I want to know how can I do this:

Query q = em.createNamedQuery("AnyQuery");
q.getResultList();

without the use of EntityManager. Any ideas?

1条回答
迷人小祖宗
2楼-- · 2019-07-11 08:15

The question itself shows that you don't understand any of the technologies you try to work with. You probably need to study some more general stuff before you do any actual development.

  • you are probably confusing JTA and JPA,
  • your statement about RESOURCE_LOCAL is not true (and irrelevant) - there are JTA and RESOURCE_LOCAL transactions and in Java EE you usually use the former,
  • your thought of using Named JPA queries without EntityManager is plain absurd and probably stems from misunderstanding of some kind (what would be the point of using named queries without an entity manager?),
  • saying "some elements from database" shows that you can't really tell the difference between records and mapped objects, in which case you probably should not use JPA at all.

I am not really expecting that you accept this answer. That's just my frustration taking over.

EDIT

OK, now that you mentioned JSF I understand more of your problem.

I assume you want to use JPA. In such case you have a choice of:

  • creating your own EntityManager (in such case you cannot have it injected; instead you have yo use an EntityManagerFactory and build your own). This is an "application managed EntityManager". You don't really want to do this.
  • using an injected EntityManaged ("conatiner managed EntityManager"). This is the standard choice.

Now you need a transaction. Since you should be using a JTA EntityManager, you will need a transaction object that is responsible for coordinating the whole thing. Again, you have two choices:

  • in a JSF bean, inject a UserTransaction (using @Resource annotation). This is messy, error-prone and takes a lot of boilerplate, but you will find all the necessary methods. You can create your own (application managed) EntityManager, call its joinTransaction method and then call begin-commit on UserTransaction. This is would be an "application managed transaction"
  • move your EntityManager code to EJB. It only takes a couple of lines of code and a single annotation (@Statless). All the code inside an EJB is - magically - wrapped inside a transaction that the container manages for you. This is the "container managed transaction" - the default and common choice.

Each of the things above could (and should) be expanded with some additional information. But the short path for you is:

  • create an EJB (a simple class with @Stateless annotation),
  • move the method that uses EntityManager to the EJB,
  • inject the EJB into your managed bean (using @EJB annotation) and call the relevant method.

The JTA transaction will happen around each call to any EJB method. This should get you started :-)

查看更多
登录 后发表回答