This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes Trying EJB3 from Eclipse WTP Book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Trying EJB3 from Eclipse WTP Book" Watch "Trying EJB3 from Eclipse WTP Book" New topic
Author

Trying EJB3 from Eclipse WTP Book

Dale Seng
Ranch Hand

Joined: Mar 22, 2004
Posts: 275
I'm trying to work through the EJB examples from the "Eclipse Web Tools Platform" by Naci Dai, Lawrence Mandel, and Arthur Ryman. The book is written with old stuff (EJB 2.1), but it hints at how to do EJB3.

I started with old stuff*, but upgraded to new stuff** when I got stuck on a JNDI error the second iteration in chapter 8.

So the problem is that when I follow the book to create a session bean (skipping xdoclet of course), I get empty local and remote interfaces created in my EJBClient project. So when it comes to using those in my JSP, there's no methods to call. Obviously, since the book was written with EJB2.1 tools, it's not going to tell me. And although an EJB 3.0 version of the "LeagueFacade" stateless session bean is listed in the book (page 357***), I can't figure out what I should be doing in my client project and in my JSP. The 2.1 way was to use a "Util" class and call .getHome().create(). But that's not the approach anymore, and there's no help in the book for the new alternative.

So, anybody using WTP to make session beans that can nudge me in the right direction?

--Dale--



* old stuff: (Eclipse 322, WTP 1.5, Websphere community 1.1)
** new stuff: (Eclipse Ganymead 3.4.1, includes WTP 2.1, Websphere community edition 2.1)
Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
    
    5
Dale,

In a JSP, you will need to use straight JNDI lookup. In a Servlet or JSF backing bean, you can use @EJB. IMO, with EJB 3.0, Eclipse WTP actually isn't all that valuable (except for the JPA mapping UI).

Regards,
Reza


Independent Consultant — Author, EJB 3 in Action — Expert Group Member, Java EE 6 and EJB 3.1
Dale Seng
Ranch Hand

Joined: Mar 22, 2004
Posts: 275
Thanks for the reply. I think I'm just going to quit working on this example (Chapter 8 of Dai, et. al.). I got to the point of deploying an app using the plain JNDI lookup, as you suggested, but when I tried to start the server that I had just downloaded (I called "new stuff" above), the server crashed on startup. Since I don't have a good EJB3 server installed, and this book isn't going to help me get through the EJB3 stuff, I think I'll just forge ahead.

--Dale--
Dale Seng
Ranch Hand

Joined: Mar 22, 2004
Posts: 275
I decided to poke around a bit more to get the EJB3 example working on Eclipse 3.4 with Geronimo 2.1. The wizards seem to create some deployment files that need to be deleted or the project wouldn't publish, and my EAR project didn't have a application.xml at all, so I had to key one. Also, the example in the book uses an EJBClient project, but I just didn't see any value add having the extra project, so I didn't go that route.

So, if anyone comes along and tries to get the EJB3 version of the Dai et. al. book working under a newer version of Eclipse and WTP, I'd suggest following the book as much as possible, then compare to my project when it doesn't work :

A zip file with EJB3 Eclipse Projects from Chapter 8 of Dai et al

This example, also doesn't lend itself to using @EJB since that isn't allowed in a jsp page. I did create a servlet version with @EJB and it worked great, but I wanted to follow the book as much as possible in the application I posted in the zip file.

If an EJB3 expert feels like looking at it there IS a problem with the application. Page 357 has an init() method marked with @Init, but it doesn't seem to run. I've commented it in the code. Although it works, I'm sure it's not done "right", so would take any suggestions.

--Dale--

[ November 20, 2008: Message edited by: Dale Seng ]
[ November 20, 2008: Message edited by: Dale Seng ]
Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
    
    5
Dale,

Good work! Haven't looked at it in great detail, but my guess is you want @PostConstruct instead of @Init. There is no EJB 3 @Init annotation :-).

Regards,
Reza
Dale Seng
Ranch Hand

Joined: Mar 22, 2004
Posts: 275
Reza, You're quick! I've just replace the zip file because I realized the first time I left out the LeaguePlanetModel project, then I "fixed" it but put that project in the root of the zip (without a folder). I think I have it right now.

Good to know about @Init and EJB3. I didn't check any references, I was just going with the seat of the pants. No wonder it didn't work!

--Dale--
Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
    
    5
Dale,

No problems, it's my pleasure to be able to help.

I understand the compulsion to "just try it". For better or for worse, I find a lot of people doing the same with annotations. While that makes it easier, there's still no substitute to doing your homework first :-).

Regards,
Reza
Dale Seng
Ranch Hand

Joined: Mar 22, 2004
Posts: 275
You are so correct about doing homework first...It is the most efficient path.

Somehow I can't just go through this book (only EJB 2.1) with a EJB3 capable toolset on hand, without wanting to see if I can get it working the new way. Although this book has a lot of wisdom about how to develop web applications, having it written with old WTP and old EJB versions is a serious disadvantage. I think it actually makes it worse when they "tease" with only a few sentences that indicate you can get newer versions of your toolset and make the example project work there, and it will be so much cooler. I guess I'll just have to get a true EJB3 book ;)

In the mean time, I did get one more iteration of the project working. But, like the earlier version, I don't think the EJB3 stuff is right (an @Resource seems not to do anything). But most of what I was up against this time was using mysql instead of Derby, and the current Geronimo instead of (whatever they used in the book). I'm going to take this topic down a winding path now by laying out how to get this iteration working on Geronimo. The project has a little Hibernate stuff in there that I've just started with (not to be considered at all correct). The project works, albeit not using hibernate.

Here's the project:
Place to download Eclipse WTP Ch 8 Iteration 3 For Ganymede, Geronimo, MySql

I used Geronimo 2.1 as my container, and I used mysql as a database for this project. It seems that you MUST start Geronimo, log into the admin console (username:system, password:manager),and create a database pool (I called mine "jdbc/MyDataSource"). It seems that just putting the mysql connector jar (with the com.mysql.jdbc.Driver class) in the xml deployment descriptors is not enough. A database pool is required. I actually placed the mysql connector jar file in the appropriate directory under "repository" before creating the pool. Then, after you have configured the databse pool, you can click "Usage" from the list of pools, and it will tell you exactly what needs to be in web.xml and geronimo-web.xml.

Also, The maven-style paths under "repository" can be troublesome. Your jar file needs to have a version number on it (ie mysql-connector-java-5.1.7.jar). If you defined your "groupId" as "mysql", then under "<GERONIMO_HOME>/repository/", you would have a "mysql" directory (if you are unfortunate enough to name your "groupId" "com.my.stuff", you would need to nest "com/my/stuff"). Inside of "mysql", you need to create a directory matching the "artifactId", and inside of that, you need to make a directory matching the version (in this case "5.1.7"). Finally, you put your jar file in there. So we have:
"<GERONIMO_HOME>\repository\mysql\mysql-connector-java\5.1.7\mysql-connector-java-5.1.7.jar"
I'm sure glad this is simplified to such a great extent!
Now, you would think that the following dependency would work:

.... but you would be WRONG!

You need to include things something like this in the geronimo-web.xml:
...



and in web-xml:

(as I mentioned above, the Geronimo Console will give you these details after you define the pool).

Here are some of the things I saw in my trials, for the googlers of the world:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Missing dependency exception
Unable to resolve resource reference res-ref-name
Missing dependency: mysql/mysql-connector-java
java:comp/env/jdbc resource-ref

--Dale--
[ November 23, 2008: Message edited by: Dale Seng ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Trying EJB3 from Eclipse WTP Book
 
Similar Threads
eclipse ejb ?
Observing Stateful Session Bean @PrePassivate and @PostActivate
Error:CalculatorBeanRemote not bound
Best way to create JSP in Eclipse
Using a regular bean in a statefull Session bean