• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

NullPointer with EntityManager in Entity Bean

 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
jason adam
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic