File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes EJB3.0, mySQL, Queries & Entity Beans? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJB3.0, mySQL, Queries & Entity Beans?" Watch "EJB3.0, mySQL, Queries & Entity Beans?" New topic
Author

EJB3.0, mySQL, Queries & Entity Beans?

Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
I was wondering if someone can help me with my EJB3.0 problem!?!?...

I have looked high and low but I can not find a reason why this code does not work (based on what I can find online).
It is a simple test webapp based on sample code I have found on the Internet but it still doesn't work (in fact I'm suprised how many samples online don't work).

The app uses a very simple mySQL DB whish is called 'test' which has one table called 'tbl_user_details' with
id (auto gen INT)
fname (varchar)
sname (varchar)

For testing purposes it has the following data
id fname sname
--------------------
1 simon one
2 paul two
3 john three
4 simon four
5 jack five

The system is in two parts TestClient (Servlet, JSP, etc) / TestEjb (Session Bean, Entity Bean, etc)

Here are some 'snippets' of code, these I believe are the relivent parts of the classes (minus all the imports and stuff)...

TestServlet (on TestClient)


TestInterfaceRemote (On TestEjb, but with classes JARd and in TestClient lIB folder)



Stateless Session Bean (on TestEjb)



TestEntity (on TestEjb)



Under the TestEjb META-INF folder I have
sun-ejb-jar.xml



persistence.xml


Now my understanding is that when I run the system, the Bean should seach the DB for all records with fname = 'simon', which means it should find three results.
My understanding is that it should then create 2 instances of TestEntity (presuming that TestEntity has the same mapped tables as the DB/tbl_user_details) for each result returned....
But try as I might everytime I run this system, I get the system to return any results!

The System.out.println("test size: "+tester.size()); in TestBean is always coming back with '0'.

The Client is calling the EJB because the System.out.println("output: "+rem.testConnection()); returns the String output.

Firstly, am I understanding the way this works correctly, secodnly (if so) why will it not work? arh!!! :-(

Any help... PLEASE

KS

[ October 17, 2008: Message edited by: Keith Seller ]
[ October 18, 2008: Message edited by: Bear Bibeault ]
Reza Rahman
author
Ranch Hand

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

I could not follow all of what you said exactly, but it sounds like you might need to do a 'flush' before doing a query. Remember that changes to the database aren't actually propagated until the method ends and the transaction is committed.

Cheers,
Reza


Independent Consultant — Author, EJB 3 in Action — Expert Group Member, Java EE 6 and EJB 3.1
Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
Yes it was a bit of a long winded post, written late at night and quickly before my laptop battery went flat! :-D

I was going to re-write it in a cut down form but never got round to it.

Basically I have a EJB webapp with remote interfaces which I know is all working and I know the client can see the Entity Bean via the remote Session Bean because I have a simple test method which outputs a string which is beign returned.
Now I want to look at connecting to a DB and doing some queries. I am using Sun WebApp 9.0 (Glassfish thingy) and have created the DB connection and Connection Pool through that (which appears to be working).

I have an Entity Bean

TestEntity


And a Session Bean (connected to a Servlet via Remote Interface)




As I say the connections are all working between the Entity Bean, Session Bean Interface and Servlet because I am getting no 'Class not found' type errors and I presume the DB connection is working because I'm getting no errors about not being able to find the DB or table or anything, so I guess that's all fine. But when I run the App I get a



Error, which suggests to me that for some reason the query isn't finding row 1.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10263
    
168

Are you sure you have posted the correct code and the exception stacktrace here? The exception stacktrace is pointing to some other entity HelloEntity which you haven't posted. It also says:

Invalid query key [sFname] in expression.


which i think is because you have an incorrect JPA query.


[My Blog] [JavaRanch Journal]
Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
Sorry! Wrong error, that one was 'easy' to fix :-)

My query is:


Database




I was expecting it to find 1 result for row 1.
[ October 18, 2008: Message edited by: Keith Seller ]
Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
Basically I'm just trying to get a working EJBQL to retrieve results rom my mySQL DB.
Reza Rahman
author
Ranch Hand

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

Your code that you posted really needs quite a bit of cleanup-up. What reference/code examples are you using? Here are just a few things I am noticing:

* There is no need to have duplicate mappings on both setters and getters. That might be confusing your persistence provider.
* The @Stateless and @Remote annotations belong on the class. Indeed, it's best to use the @Remote annotation on the class.
* There is no need to inject a entity manager factory in a managed environment. You can directly inject the entity manager instead.
* As I mentioned in the previous post, see if you need to use an manual flush.

From the looks of the code, I would ask you to slow down and take a deep breath. There might be other things in your applications that are not quite right. In particular, if you come from a EJB 1.0 or EJB 2.x background, it's easy to make mistakes. EJB 3.0 is really worlds apart from the earlier versions. It's important to slow down and solidify your understanding of the syntax first as to not spend hours pulling your hair out, stumped by relatively avoidable problems. Taking a little bit of time initially instead is well worth the investment in time.

Hope it helps.

Cheers,
Reza
[ October 18, 2008: Message edited by: Reza Rahman ]
Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
Cheers for that.

I have been going through what you said and have been cleaning up the code, I have one question though

"The @Stateless and @Remote annotations belong on the class. Indeed, it's best to use the @Remote annotation on the class."

I'm not sure what you mean by this?

I come from a sort of EJB 2.1 background, but it's from some 5-6yrs ago.
As I say, I've looked around the Net but found few real reliable examples of EJB3.0, most seem to merely be people's person examples and they seem to vary greatly in the way they use EJB3.0.

Do you know of any good samples/examples/tutorials out there for EJB3.0 with DB access I could resource?
[ October 20, 2008: Message edited by: Keith Seller ]
Reza Rahman
author
Ranch Hand

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

If budget is not an issue, all of the leading books on EJB 3.0 are pretty good. The Java EE 5 vendor you are using should have decent tutorials too. Here is the JBoss EJB 3.0 tutorial as an example: http://docs.jboss.org/ejb3/app-server/tutorial/ (by no mean the only one).

Cheers,
Reza
Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
OK, cheers for that. I will have a look at it in a bit, but for now I seem to have taken a step backwards!

I decided to 'start again' with my WebApp because it was getting messy and maybe I was trying to do a little too much in one go.
Below is the following:
EjbCleint
- TestServlet
- ConnectionManager (returns the InitialContext)
- web.xml

EjbServer
- TestEntity
- TestSessionRemote (copy also compiled and JARd into the Client's lib folder)
- TestSessionBean
- persistence.xml
- sun-ejb-jar.xml

And finally, my (yet another) error!

Why-O-why can my client not see the TestSessionBean when fundamentally it's the same code, just tidyed up and with better class naming?


This is gettign VERY frustrating to say the least, I have now stepped backwards from when I coupldn't connect to the MySQL DB as now I
can not even connect to the Remote Bean and as I say, it's the same damn code!!!

TestServlet


ConnectionManager


web.xml


TestEntity


TestSessionRemote



TestSessionBean


persistence.xml


sun-ejb-jar.xml


Server.log error
Reza Rahman
author
Ranch Hand

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

* Take a look at the JNDI context to see what the bean is actually named.
* Why not simply use @EJB on the servlet? I'm assuming the web container and the EJB container are on the same VM? Look-ups are inherently messy and should be avoided. It may also be that your JNDI factory parameters are incorrect. In the same VM, it is sufficient simply to instantiate the JNDI context. The container is responsible for taking care of all the rest.
* It is necessary to make the EJB remoteable? Why not just use @Local?
* Other than the persistence.xml, all the XML DD you have is not really needed, neither are the resource refs in the web.xml. That might be messing you up as well. Note, these were needed pre-EJB 3.0.

Cheers,
Reza
[ October 20, 2008: Message edited by: Reza Rahman ]
Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
OK, rather horrifyingly, it seems Log4j was killing it!
Reza Rahman
author
Ranch Hand

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

Let me know if you continue to struggle. If this is the full extent of your current code, I can certainly post a version that would work under GlassFish (looks like that's what you are using?).

Please do make an honest effort yourself though, including looking at a few references. As such, there is a lot on my plate and doing anything incremental at the moment would take away from something else that someone else thinks is equally urgent :-(. Trust me, EJB 3.0 is a breeze and can produce very clean, agile code one you "get" the syntax and a few deployment rules (the last of which is being relaxed in EJB 3.1).

Cheers,
Reza
Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
Sure will do, the best way to learn after all is by doing. You're right I'm working under Glassfish, but have found quite a few of the examples around have been less than reliable and this has become something more of a mish-mash based on what I've found that works and doesn;t (hence the reason my code needed so much tiying up).

I'm really using it because for the past few years I have been using merely JSE, and recently wrote a system in standrad java which works, but I thougght really should have been written using a 'prper' standard so adopted EJB3.0 as I had used in my first job EJB2.1 (about 4-5yrs ago as I say0.

I'm sure once I manage to break down the initial barriers, EJB3.0 will fall into pllace, ujnfortuneatly it seems to have taken something of an effort to break down the first part(s).

If I can get a basic DB query working I think that'll be my big step.

PS: I think I will have to give this "EJB In Action" book a look over, save with all the forum posts!
[ October 20, 2008: Message edited by: Keith Seller ]
Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
...I can certainly post a version that would work under GlassFish...

I would greatly appreciate this, does it include much EJBQL? As that is my next 'baby step', for some reason all my queries are returning 0 results.
Reza Rahman
author
Ranch Hand

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

OK..I will see when I can make time for this...do send me what you have in a zip file, it'll make it easier. My email is reza_rahman@lycos.com.

In the meanwhile, you are welcome to download the EJB 3 in Action example code. It is freely available from the Manning site.

Regards,
Reza
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10263
    
168

Originally posted by Keith Seller:
for some reason all my queries are returning 0 results.


Keith,

I don't know how much useful this one is going to be, but i just noticed this interesting post from James Sutherland about Toplink (that's what you are using in your example).
Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
Originally posted by Jaikiran Pai:


Keith,

I don't know how much useful this one is going to be, but i just noticed this interesting post from James Sutherland about Toplink (that's what you are using in your example).


Cheers for that, I'll give it a try (or maybe switch to Hibernate :-D )
Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
Just like to say thanks to Reza for looking at that sample I'd done, I tweaked it slightly to work with Remote interfaces, but now I'm not sure if they are worth using so I might retreat back to the Local as you sent over.

This is because I have an issue with search results that return multiple results on Remote interfaces.

I get the following error, which I think has something to do with LAZY FETCHING!?

I have the toplink-essentials-agent.jar and toplink-essentials.jar in the EJB LIB folder (as reading somewhere) but still get the same error.


[ November 18, 2008: Message edited by: Keith Seller ]
Reza Rahman
author
Ranch Hand

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

Not sure exactly. I'd try tinkering.

Thanks,
Reza
Kevin P Smith
Ranch Hand

Joined: Feb 18, 2005
Posts: 362
Opted to go for the Local Interface approach instead as it was working!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EJB3.0, mySQL, Queries & Entity Beans?