Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Home look up

 
Prakash Krishnamurthy
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am confused on how the look up for a (remote) home object should be done.
Should it be
initialContext.lookup(java:comp/ejb/CustomerHome);
or should it be just be
initialContext.lookup("CustomerHome");
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"CustomerHome" is a real JNDI name; the name with which you deployed the EJB into the application server.
"java:comp/ejb/CustomerHome" is a private JNDI name that you need to map into a real JNDI name using the EJB deployment descriptors.
 
Prakash Krishnamurthy
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so then when you are writing a EJB client to access the EJB's, I would think that, since you map java:comp/env/ejb/CustomerHome using the deployment descriptors, all you need to do in the client code is
initialContext.lookup("CustomerHome");
Am I right?
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope (check what he said, Lasse). In your client code you would ONLY refer to initialContext.lookup("java:comp/env/ejb/CustomerHome"). That way regardless of what the real JNDI name is changed to your client code never changes...
Kyle
 
Prakash Krishnamurthy
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kyle Brown:
In your client code you would ONLY refer to initialContext.lookup("java:comp/env/ejb/CustomerHome").


Kyle,
I am confused now. Let me re-ask this question.
I came across a multiple choice question which was
Which of the following is the best way to find the Home Object. Choose one.
1) Object obj=initialContext.lookup("java:comp/env/ejb/CustomerHome")
2) Object obj=initialContext.lookup("CustomerHome")

I looked through "Mastering EJB-Ed Roman" and all the client code had option 2 there. It said that you give your home object a "nick-name" and then always do a look-up on that nick name. But then I did not find anything wrong with option 1.
What do you think?
PK
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Option (1) was all that was available prior to EJB 1.1 and thus all older code samples use it. However, Option (2) is to be GREATLY preferred for all new code.
Kyle
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic