This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes NullPointer with EntityManager in Entity Bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "NullPointer with EntityManager in Entity Bean" Watch "NullPointer with EntityManager in Entity Bean" New topic
Author

NullPointer with EntityManager in Entity Bean

jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Using JBoss 4.2.1, Java 6 and JEE 5.

The basic structure of the offending code is I have a stateless bean using the EntityManager to get a set of entity beans. Those entity beans are using @PersistenceContext.

I have a @PostLoad method in the entity that also uses the EntityManager to get some other beans (I'm going to look into a join, but the fields I need to search over are not the primary key fields so it isn't a standard relationship. If anyone knows how to do that with annotations I'm all ears).

Problem is, the session bean seems to be getting the first set of entities just fine. However, the EntityManager in my entity bean is not getting injected; I get a NullPointerException trying to use it in the @PostLoad.

One thing to note is that the first entity is a subclass of a @MappedSuperclass. I don't think that should have bearing on this, but I could easily be wrong.

So pseudo-code


So all A's contain a list of related B's (but again this is shared by a data value other than the keys so not sure how to map that with annotations yet).

Debug also shows that EntityA is getting populated with all the data from the database, so I know that that is not an issue. And the session bean is using the EntityManager with no problems so my configuration appears correct.

Is this a violation of the spec or something that I don't understand (which is a lot it seems ), or is what I'm presenting viable and there is some other dastardly issue afoot here??

Thanks!
Jason
[ December 04, 2008: Message edited by: jason adam ]
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10146
    
165

EntityManagers cannot be injected in Entities. As per the spec:

The container is responsible for managing the lifecycle of container-managed persistence contexts, for injecting EntityManager references into web components and session bean and message-driven bean components, and for making EntityManager references available to direct lookups in JNDI.


Just curious - In your example, A has a collection of B and in the PostLoad, you are trying to fetch some B's through entitymanager. Why not just do this.getBs() ?
[ December 05, 2008: Message edited by: Jaikiran Pai ]

[My Blog] [JavaRanch Journal]
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Oops, I forgot to annotate B with @Entity

B's are also entities. I would really love to figure how to use a join here, but it again the fields used between the two are not keys. For joins, at least in my limited knowledge, a field in one table is a key for the other. I was getting some weird SQL exception when I tried messing with joins so I thought I'd first try just querying for them, then I'd go from there.

Thanks for the info about not injecting into entities! Looks like I'll have to resort to good ol' JNDI at first.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullPointer with EntityManager in Entity Bean