Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

How to pass an entity class in remote method invocation from a Enterprise Client application to EJB  RSS feed

 
Greenhorn
Posts: 2
1
Chrome Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Having passed my Java Programmer II exam last month I am now diving into the world of java EE, meaning I am quite a newbie on both Java and EE.
Currently I am trying to implement an example from the book "Beginning Java EE 6 platform with glassfish"  from scratch with NetBeans.
The goal is to create a stateless enterprise bean BookEJB that implements a remote interface with createBook(book) and deleteBook(book) methods.
CreateBook should persist the book in a mySql database and deleteBook should delete it from the database.
An Enterprise Application client will remotely call createBook and deleteBook.
Book is an entity class.

I am following the process described in https://netbeans.org/kb/docs/javaee/entappclient.html.
However this example does not involve passing an entity class while invoking the remote methods.
This is exactly where I am getting into trouble..

What I have done:

1) Create a new Java  - Java Class Library project "BookRemote" for the remote interface, e.g.



2) Create a Java EE - Enterprise Application project named "BookEnterpriseApp" with a EJB module "BookEnterpriseApp-ejb"
3) Create a new glassfish JDBC resource on the enterprise app and import the resulting glassfish-resources.xml as a resource on glassfish. Ping successful.
4) Create a new stateless session bean inside BookEnterpriseApp-ejb, called BookEJB while checking checkbox remote in project BookRemote:



4) Create the Book Entity.



I decided to add the Book entity inside the BookRemote project, since it is needed in the interface methods that I need to add:



I did not create a persistence.xml in BookRemote since when I try that I cannot choose the JDBC resource I have added earlier.
This results in warning "project does not contain a persistence unit" on the line @Entity in Book.java.

5) Add a persistence.xml inside BookEnterpriseApp-ejb that refers to the jdbc resource:



6) Create a new Java EE - Enterprise Application Client project named "BookClient", and call BookEJB methods as follows:



7) Deploy and run the client app.

This results in exceptions:
...
java.lang.reflect.InvocationTargetException
...
Caused by: java.lang.IllegalArgumentException: Object: avt.book.entity.Book[ id=null ] is not a known Entity type.


My Questions:
- Should I indeed add the book entity inside the BookRemote project?
- if yes, what should I change to prevent the execption?



 
Creator of Enthuware JWS+ V6
Bartender
Posts: 3121
259
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

My Questions:
- Should I indeed add the book entity inside the BookRemote project?
- if yes, what should I change to prevent the execption? 


Yes, you need to add the Remote Interface and all the dependent classes (e.g. superclasses, classes used as method parameters) in the client-jar.

Application client jars are always tough to get it right because it needs a lot of application server dependent configuration. Why don't you try a Servlet? Just have a look at my example (chapter 10) in the EJB 3.1 notes: OCEEJBD-Links.
 
Arno van Haastert
Greenhorn
Posts: 2
1
Chrome Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Frits,

Thanks for your quick reply.

I think the next chapter of my book also will go into web applications which probably is a much more common use case than the client application..

In the mean time I somehow managed to make it work by unchecking the "include all entity classes" and adding explicitly the fully qualified Book class in the persistence.xml on BookEnterpriseApp-ejb.
This was suggested in some posts I came across this afternoon but before it resulted in not being able to deploy the BookEnterpriseApp-ejb.

The only thing I can think of doing differently this time is that I initially created the book entity inside BookEnterpriseApp-ejb and then dragged it (refactored it) into the BookClient project.
So still too much hocus-pocus to be completely satisfying but still a happy end of a day of trying ;-)

Next week I might just try once from scratch to see if I can make it work at once.



Regards,

Arno
yess.jpg
[Thumbnail for yess.jpg]
 
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender
Posts: 3121
259
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice! Well done, and thanks for reporting your solution. (have a Cow!)

So still too much hocus-pocus to be completely satisfying but still a happy end of a day of trying ;-) 


Netbeans has some nice wizards and takes a lot of configuration away from the user which can be a pain. My advice is to check after you are finished what classes are in the EAR, WAR or JARs created by Netbeans.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!