• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help with EJB3.0 Stateless Session Bean code

 
Ranch Hand
Posts: 59
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Recently in an interview they asked me to develop code for the following scenario:

Student: ID, Name
Course: ID, Name
A student can take many courses and a course can have many students.
1).Write skeleton code of entity bean (or whatever technologies you like) classes to model Student and Course and student course registration.
2). Write a skeleton Student DAO class that support
2.1). add a new student along with their course registrations.
2.2). Delete a student.
2.3).Get all students, sorted by their name, for a given course with course name as input.
Bonus Points:
2.4). What if we want to record course scores?  What possible changes need to be made?
                       Explain briefly.
2.5). How to find all students who don’t register for a given course?  

I am a self-learner and a newcomer to EJB3.0, I wrote the code as given in the attachments. They failed me. What have I done wrong?

This is my EJB3.0 Stateless Session Bean that caters to the business requirements:




Here are my 2 Entity Beans: Student and Course



 
Ravi Desigan
Ranch Hand
Posts: 59
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are my 3 tables, STUDENT, COURSE AND STUDENT_COURSE:

 
Creator of Enthuware JWS+ V6
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am a self-learner and a newcomer to EJB3.0, I wrote the code as given in the attachments. They failed me. What have I done wrong?


Did they tell you why they weren't happy with your effort?

I find it a brave attempt but I have some questions/remarks:

  • where is your DAO  "2). Write a skeleton Student DAO class that support"
  • In a bidirectional @ManyToMany relationship you need to designate an owner (by the mappedBy element of the annotation) otherwise you create 2 unidirectional relationships
  • Why do you have try-catch blocks?


  •  
    Ravi Desigan
    Ranch Hand
    Posts: 59
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hello,

    Thanks for the reply.

    I find it extremely tough to learn EJB3.1 on my own, because I am unable to set-up a successful test environment using Software/tools.

    I have read a lot since the above interviewers failed me (I couldn't ask them for clarifications as they had sent me home this test via some recruiters) and I changed my entities to what it appears like below in quotes.

    I think they mean by DAO class, the Session bean which will access the database using NamedQuery, NativeQuery and such. I am still working on the right version of this, but have attached the version I started with in the original reply in quotes.

    Please see my newly modified entities, I have included mappedBy clause as per your valuable suggestion.

    The STUDENT Entity Bean:



    The COURSE Entity bean:

     
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Posts: 3411
    320
    Android Eclipse IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    I find it extremely tough to learn EJB3.1 on my own, because I am unable to set-up a successful test environment using Software/tools.


    I could help you with this, what have you been using until now? And where did you get stuck? I am using the following tools:

  • Eclipse (Juno)
  • Derby database (db-derby-10.11.1.1)
  • Squirrel - to check the database


  • I think they mean by DAO class, the Session bean which will access the database using NamedQuery, NativeQuery and such.


    The DAO normally is a seperate class in a seperate layer of your application. I would suggest using a CDI bean for this (EJB is to heavy for this and the overall transaction is managed from your CourseManagerBean).

    You would get something like this:


    Another question: have you thought about cascade options?
     
    Ravi Desigan
    Ranch Hand
    Posts: 59
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Oh, if you or someone could help me with Software setup, that would be the best award for me!

    I have installed Eclipse - Indigo, MYSQL database and Jboss application server version 7.1.1 Final.

    Initially I had installed Eclipse Neon, but on learning the hardway it does not work with JDK1.8, I uninstalled it. And then I installed Eclipse Indigo.

    Now I am able to deploy my EJB into Jboss 7.1.1 server under Eclipse Indigo and am able to connect with database.

    But on client EJB launch as Java Application, I am getting a SASL exception which says, all authentication mechanisms failed.

    This is the exact error I am getting:

    ERROR: JBREM000200: Remote connection failed: javax.security.sasl.SaslException: Authentication failed: all available authentication mechanisms failed
    javax.naming.NamingException: Failed to create remoting connection [Root exception is java.lang.RuntimeException: javax.security.sasl.SaslException: Authentication failed: all available authentication mechanisms failed]
    at org.jboss.naming.remote.client.ClientUtil.namingException(ClientUtil.java:36)
    at org.jboss.naming.remote.client.InitialContextFactory.getInitialContext(InitialContextFactory.java:121)
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:684)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
    at javax.naming.InitialContext.init(InitialContext.java:242)
    at javax.naming.InitialContext.<init>(InitialContext.java:216)
    at jayashree.statestreettest.clientutility.JNDIlookup.getInitialContext(JNDIlookup.java:22)
    at jayashree.statestreettest.client.CreateStudentClient.doLookup(CreateStudentClient.java:58)
    at jayashree.statestreettest.client.CreateStudentClient.main(CreateStudentClient.java:40)
    Caused by: java.lang.RuntimeException: javax.security.sasl.SaslException: Authentication failed: all available authentication mechanisms failed
    at org.jboss.naming.remote.protocol.IoFutureHelper.get(IoFutureHelper.java:87)
    at org.jboss.naming.remote.client.NamingStoreCache.getRemoteNamingStore(NamingStoreCache.java:56)
    at org.jboss.naming.remote.client.InitialContextFactory.getOrCreateCachedNamingStore(InitialContextFactory.java:166)
    at org.jboss.naming.remote.client.InitialContextFactory.getOrCreateNamingStore(InitialContextFactory.java:139)
    at org.jboss.naming.remote.client.InitialContextFactory.getInitialContext(InitialContextFactory.java:104)
    ... 7 more
    Caused by: javax.security.sasl.SaslException: Authentication failed: all available authentication mechanisms failed
    at org.jboss.remoting3.remote.ClientConnectionOpenListener$Capabilities.handleEvent(ClientConnectionOpenListener.java:365)
    at org.jboss.remoting3.remote.ClientConnectionOpenListener$Capabilities.handleEvent(ClientConnectionOpenListener.java:214)
    at org.xnio.ChannelListeners.invokeChannelListener(ChannelListeners.java:72)
    at org.xnio.channels.TranslatingSuspendableChannel.handleReadable(TranslatingSuspendableChannel.java:189)
    at org.xnio.channels.TranslatingSuspendableChannel$1.handleEvent(TranslatingSuspendableChannel.java:103)
    at org.xnio.ChannelListeners.invokeChannelListener(ChannelListeners.java:72)
    at org.xnio.nio.NioHandle.run(NioHandle.java:90)
    at org.xnio.nio.WorkerThread.run(WorkerThread.java:184)
    at ...asynchronous invocation...(Unknown Source)
    at org.jboss.remoting3.EndpointImpl.doConnect(EndpointImpl.java:270)
    at org.jboss.remoting3.EndpointImpl.doConnect(EndpointImpl.java:251)
    at org.jboss.remoting3.EndpointImpl.connect(EndpointImpl.java:349)
    at org.jboss.remoting3.EndpointImpl.connect(EndpointImpl.java:333)
    at org.jboss.naming.remote.client.EndpointCache$EndpointWrapper.connect(EndpointCache.java:105)
    at org.jboss.naming.remote.client.NamingStoreCache.getRemoteNamingStore(NamingStoreCache.java:55)
    ... 10 more
    Exception in thread "main" java.lang.NullPointerException
    at jayashree.statestreettest.client.CreateStudentClient.main(CreateStudentClient.java:42)



    I have spent days trying to get the server work with MYSQL database and then be able to deploy my EJB. Now this! I feel like hitting on a wall.
     
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Posts: 3411
    320
    Android Eclipse IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    But on client EJB launch as Java Application, I am getting a SASL exception which says, all authentication mechanisms failed.


    The easiest way to connect as a client to an EJB is by using a Servlet. An Application client is tricky and needs some extra configuration.

    Just create a Dynamic Web Project in Eclipse and inject the EJB in the Servlet. For example:



    And call your EJB from a browser and provide the parameters in the URL, for example:

    http://localhost:8080/StudentCourseProject/StudentCourseServlet?id=5&name=student&course=maths
     
    Ravi Desigan
    Ranch Hand
    Posts: 59
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    That is really a good suggestion. I tried to set up such an environment (that is, also including a Web project with servlet) in my Jboss environment yesterday, but there also I got some exceptions. Will try some more. Thank you very much.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic