JTA& MySQL - 如何从数据库中检索记录 [英] JTA & MySQL - How to Retrieve records from database

查看:139
本文介绍了JTA& MySQL - 如何从数据库中检索记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JTA的新手,我需要一种从数据库中检索一些元素的方法。我可以通过EntityManager执行此操作,但它仅适用于ResourceLocal。
我想知道如何做到这一点:

 查询q = em.createNamedQuery(AnyQuery); 
q.getResultList();

不使用EntityManager。
任何想法?

解决方案

问题本身表明您不了解任何您尝试工作的技术用。您可能需要在进行任何实际开发之前先学习一些更一般的内容。




  • 您可能会混淆JTA和JPA,

  • 您关于RESOURCE_LOCAL的声明不是真的(不相关) - 有JTA和RESOURCE_LOCAL事务,而在Java EE中,您通常使用前者,

  • 没有EntityManager命名的JPA查询是荒谬的,可能源于某种类型的误解(在没有实体经理的情况下使用命名查询是什么意思),

  • 说数据库中的某些元素表示你不能真正地告诉记录和映射对象之间的区别,在这种情况下,你可能不应该使用JPA。



<我真的不希望你接受这个答案。这只是我的挫折接管。



编辑



提到的JSF我了解更多你的问题。



我假设你想使用JPA。在这种情况下,您可以选择:




  • 创建自己的EntityManager(在这种情况下,您不能注入它),而是使用一个EntityManagerFactory并构建您自己的)。这是一个应用程序管理的EntityManager。你真的不想这样做。

  • 使用注入的EntityManaged(conatiner managed EntityManager)。这是标准的选择。



现在您需要一个交易。由于您应该使用JTA EntityManager,因此您将需要一个负责协调整个事物的事务对象。同样,您可以在JSF bean中有两种选择:




  • ,注入UserTransaction(使用@Resource注释)。这是凌乱的,容易出错的,需要很多样板,但是你会发现所有必要的方法。您可以创建自己的(应用程序管理的)EntityManager,调用其joinTransaction方法,然后在UserTransaction上调用begin-commit。这将是一个应用程序管理的事务

  • 将您的EntityManager代码移动到EJB。它只需要几行代码和一个注释(@Statless)。 EJB内的所有代码都是神奇地包装在容器为您管理的事务中。这是容器管理事务 - 默认和常用选项。



上述每一项都可以(应该)扩展还有一些附加信息。但是你的简短路径是:




  • 创建一个EJB(一个带有@Stateless注释的简单类),

  • 将使用EntityManager的方法移动到EJB,

  • 将EJB注入您的托管bean(使用@EJB注释)并调用相关方法。



JTA事务将在每次调用EJB方法时发生。这应该让你开始: - )


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?

解决方案

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 :-)

这篇关于JTA&amp; MySQL - 如何从数据库中检索记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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