将实体管理器注入托管 bean [英] Injecting entity manager into managed bean

查看:22
本文介绍了将实体管理器注入托管 bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

It is possible to inject entity manager (or its factory) into jsf managed bean using @PersistenceContext (or @PersistenceUnit)?

I Tried it but nothing, I obtain a NullPointerException.

解决方案

Yes it is possible. This is the syntax.

@PersistenceContext
EntityManager em;

You need to have a persistence.xml in your project. Btw: I'm running Glassfish 3.

After this you can then use methods like em.createNamedQuery.

Also remember the injection takes place after the constructor so if your trying to do database functions in the constructor this will not work. You will have to add the @PostConstruct annotation to a method. This is probably the problem your having.

这篇关于将实体管理器注入托管 bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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