knownasilya
knownasilya

Reputation: 6143

JPA2 EntityManager is null

Code is here: http://github.com/knownasilya/Ambience

I'm testing persistence through my index.xhtml and in my Admin class I instantiate the EntityManager with the @PersistenceContext annotation. Every time I run this example I get NPE at em.persist(another);.

I have the persistence.xml setup with the same connection information as my local db configuration in Eclipse. How can I find out where the error is?

Edit Here are the relavent files.

Index.xhtml https://github.com/knownasilya/Ambience/blob/master/g5.ambience/WebContent/index.xhtml

Admin.java https://github.com/knownasilya/Ambience/blob/master/g5.ambience/src/g5/ambience/user/admin/model/Admin.java

persistence.xml https://github.com/knownasilya/Ambience/blob/master/g5.ambience/src/META-INF/persistence.xml

EDIT2 I created a UserDAO, but I still get a NPE when I try creating the EntityManager specifically on line 24

EntityManager em = emf.createEntityManager();

With this error Caused by: java.lang.NullPointerException at g5.ambience.user.UserDAO.<init>(UserDAO.java:24)

UserDAO.java https://github.com/knownasilya/Ambience/blob/master/g5.ambience/src/g5/ambience/user/UserDAO.java

EDIT3 I now have 3 layers of abstraction/encapsulation. The model (getters/setters for persistence), the business logic, and the view logic.

I have my entity factory here: http://bit.ly/HV4d11, and that's an application scoped managed bean. Then I have the ManagedUserBean http://bit.ly/In19rX which still gives me a NPE when I invoke getUserByUsername method through UserView http://bit.ly/In1ta6 (session scoped). Still no luck, the error is on the EntityManager creation, here is my stackTrace: http://pastie.org/3814236

Upvotes: 0

Views: 1495

Answers (2)

Adam Gent
Adam Gent

Reputation: 49075

The problem is you have confused the "Active Record" pattern with the "DAO" pattern.

If you want to use the "Active Record" pattern in Java your best option is to use Spring Roo or the Play framework (< 2.0).

Otherwise you will want to follow the the traditional DAO + Service pattern (I believe Roo 1.2.1 now supports this pattern).

EDIT: After circling back to this question I seriously recommend you use Spring Roo just so you can see how the different design patterns work since Roo now supports both. Also it will setup the entity manager correctly among many other things. You don't have to use Roo in the long run. Your just going to look at the scaffolding code to learn the correct way.

Upvotes: 1

Piotr Gwiazda
Piotr Gwiazda

Reputation: 12212

UserDAO needs to be a EJB bean e.g. @Stateless to use @PersistenceContext annotation. Good design pattern is to create two layers

  • Backing beans - managed beans for UI, you use them in XHTML.
  • Service layer, or DAO as you call it - stateless beans that implement basic business logic

You decide what is business logic and what is UI related logic.

If you use JEE6 CDI solution, you won't need to declare managed beans in faces-config.xml

Upvotes: 1

Related Questions