使用 JPA 更新
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
createEntityManager()
- 使用@PersistenceContext
注入一个PersistenceUnit
,但随后重新分配etm
- 我建议删除两者并参见上面的 p2。如果您根本不使用任何依赖注入,则删除所有注释,保留当前代码,然后键入:(
并在 persistence.xml 中定义
RESOURCE_LOCAL
。您确实不需要 JTA)createEntityManager()
- inject one using@PersistenceContext
PersistenceUnit
, but then re-assign theetm
- 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:
(and define
RESOURCE_LOCAL
in your persistence.xml. You really don't need JTA)好吧,代码应该没有任何噩梦......(至少对 glassfish 中的我来说)
带有
代码的
persistence.xml欢迎任何改进...(实际上需要...)
(如何使用@PersistenceContext)
谢谢
Pradyut
well the code should be without any nightmares...(atleast for me in glassfish)
with the persistence.xml having
the code
Any improvements are welcome...(actually needed...)
(How to go about using @PersistenceContext)
Thanks
Pradyut
您的“普通”类很可能不是托管组件,即其生命周期由容器管理的类(如 Servlet、Servlet Filters、JSP 标记处理程序、JSF Managed Beans...)无法从资源注入中受益1。
因此,
UserTransaction
和EntityManagerFactory
都没有注入这里,因此出现了NullPointerException
。老实说,您应该尝试使用容器管理的 EntityManager,这将使您的代码不那么混乱。如果无法注入,请通过 JNDI 查找获取。请参阅下面的资源。
1 查看 Web Tier to Go使用 Java EE 5:查看资源注入,以很好地概述可以注入的内容以及注入位置。
资源
参考
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 theEntityManagerFactory
are injected here, hence theNullPointerException
.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