Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

EJBs in helper classes - can't figure out how to do it  RSS feed

 
Brendan Healey
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Although there are lots of posts about on the subject I can't get this to work so here goes. I've got
a session bean with a container managed persistence context like this:



and a JSF managed backing bean like this:



and it's fine, I can access the database from my backing bean just fine. Then I realise that I
need a custom JSF validator that requires a new class and a new file. So I create a new class
for the custom validator:



and this results in a NullPointerException when I try to access the EJB usersFacade. I gather that with the first
@EJB reference in my code snippets I'm instantiating the EJB, and then again with the second reference (so why
doesn't it work twice?). I read somewhere that I need to do a jndi lookup from my constraint validator class but
to be honest I can't seem to get that working either.

I been experimenting with @EJB(name= and @EJB(mappedName= and @EJB(lookup= and @Stateless(name= etc...
but I can't manage to get everything in the right place at the right time to work.

If anyone can give me a helping hand here I'd be greatly appreciative. I'm using Glassfish v3.

Thanks,
Brendan.

 
Brendan Healey
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Luckily I solved the problem myself, although whether this (the solution) is the best way to do it
I don't know. So for the benefit of anyone else who's tearing their hair out after a 16 hour day
trying to get this to work this is what I did:

I've got a stateless session bean as follows:



My JSF backing bean can access the instance of the session bean (which provides database access via
a container managed entity manager) like this:



the key point here is that within the backing bean class (RegisterView) I can call session bean functions
like usersFacade.create(...) and it accesses the session bean and there is a connection to the database.

The problem starts when I need a custom validator. In my case I'm using AJAX so that when the user
clicks out of the username field on an input form the validator checks that the username is not already
in use, without having to wait for an entire form submission before finding that out. So in my Facelets
page I've got the following:



The RegisterUserValidator class implements the Validator interface, overrides the validate() function
and throws a ValidatorException if it encounters any problems.

The problem starts when you suddenly realise that, despite every expectation to the contrary, you
are unable to use @EJB to inject a reference to the specific instantiation of the session bean that you
were merrily using in your backing bean. Well I wasn't able to anyway. What I have been able to do
is to add the following code to the constructor for RegisterUserValidator:



I am then able to perform database operations via the session bean reference (usersFacade in this case).
I'll be surprised if there's not a more succinct way of doing this and if there is I'd love to hear from anyone
who can tell me what it is. Otherwise I hope this is of help to someone one day.

I'm using Glassfish v3 and the session bean is a no interface bean (new in EE 6).

Regards,
Brendan.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!