This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
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?
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)
Also check your entries in persistence.xml against your class details.
Joined: Aug 11, 2011
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.
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: