aspose file tools*
The moose likes Object Relational Mapping and the fly likes Greenhorn: JPA entitymanager is null and I don't think is suppose to be? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Greenhorn: JPA entitymanager is null and I don Watch "Greenhorn: JPA entitymanager is null and I don New topic
Author

Greenhorn: JPA entitymanager is null and I don't think is suppose to be?

Alan Dee
Greenhorn

Joined: Aug 11, 2011
Posts: 7
Ok. So I'm learning JPA on a Java EE web application using JSF. I used netbeans to create an entity and the wizard to create JSF pages (CRUD). I can use that to create entries into the database. But now, I wast to pull a specific entry from the table.
Trying to stick to convention and have the following packages: entities, boundary (session bean or facade layer) and presentation.
In my CompaniesFacade I created a method:



Anyway, the entityManager is empty. As I understand, which must be incorrect, the @PersistenceContext annotation will set the em parameter with the container-managed entity manager.

So, can anyone help me understand what I'm missing.

Thanks.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3825

What's the exact error/exception you get?


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Alan Dee
Greenhorn

Joined: Aug 11, 2011
Posts: 7
Everything works except the entity manager is null. When I run the page I get: "Cant instantiate class: my.presentation.CompanyController." (You can see the commented lines where I've tried to pull stuff.) I've included the glassfish output on the bottom for reference.

Now, if I create the entity manager manually it seems to work. But if I comment out the two lines I get the error shown in the output.txt file. Of coarse, as I understand it this is not the solution. It works and helps to narrow down the possibilities.



Again, I really appreciate your help in this. I've been trying really hard to learn JavaEE. On principle I love the methodologies but in practice I'm finding it very.... difficult.

Below is the full output. I tried to attach it as a file but it wouldn't let me?
Kumaravadivel Subramani
Ranch Hand

Joined: Jul 05, 2008
Posts: 166

Hi Alan,

Try to post your code/error/exception in short, if you are not able to upload a file then try to put printstacktrace of the exception. (you know the place where the exception occurs, so catch and print out)

As a suggestion I've two things in my mind,

1) have defined the EntityManager next line of the annotation like this, (avoid to use the EntityManager inside a function)

@PersistenceContext(unitName="EmployeeService")
EntityManager em;

2) Before try to print out the getEntityManager() try to check whether created EntityManager is null, if so then your class name defined in annotation may not be having a right one.

Just a thought..


No pain, No gain.
OCJP 1.6
Kumaravadivel Subramani
Ranch Hand

Joined: Jul 05, 2008
Posts: 166

Also check your entries in persistence.xml against your class details.
Alan Dee
Greenhorn

Joined: Aug 11, 2011
Posts: 7
Thanks for the reply. I trimmed down the output from above to focus on the caught exception.

I figured out the problem while I was preparing to post a response. I was going to post the entire lifecycle so it can be reviewed. In doing so I moved the initialisation of the companyFacade from the JSF bean constructor to the getTitle method, and that was the problem. I quess the constructor runs before the EJB container populates the @EJB annotated property. (I welcome a more expert explanation if I misunderstand)

So what I had was:


But when I moved it into the getTitle method, expecting to get an error, instead it worked.



Thanks for your help. Even though I "stumbled" upon the problem myself I did so because of your help. I hope this post will help others avoid the same mistake.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10033
    
163

Alan Dee wrote: I quess the constructor runs before the EJB container populates the @EJB annotated property. (I welcome a more expert explanation if I misunderstand)

That's correct. As per the spec, the code in the constructor of a container managed class (like this JSF managed bean) cannot rely on injections to have been completed on that object. Any such initialization of the object will have to be done in a method annotated with @PostConstruct. The container will call that method after the object has been fully injected. So you can change your code to:


[My Blog] [JavaRanch Journal]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Greenhorn: JPA entitymanager is null and I don't think is suppose to be?