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 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 ]
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 ]
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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: NullPointer with EntityManager in Entity Bean