• 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

EJB with Hibernate

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Im using netbeans ide to create a simple project using hibernate and EJBs . I am able to generate all the hbm's and java classes and EJB class uses the hibernate session to create query. When I call this method using my servlet though it says

exception

javax.ejb.EJBException: javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB

Here is the stack trace

Caused by: javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB
at com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:718)
at com.sun.ejb.containers.util.pool.NonBlockingPool.getObject(NonBlockingPool.java:200)
at com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:443)
... 41 more
Caused by: javax.ejb.CreateException: Could not create stateless EJB
at com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:526)
at com.sun.ejb.containers.StatelessSessionContainer.access$000(StatelessSessionContainer.java:90)
at com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:716)
... 43 more
Caused by: org.hibernate.HibernateException: No CurrentSessionContext configured!
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:542)
at hobernatetest.EJBTest.<init>(EJBTest.java:23)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:466)
... 45 more
|#]



 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You presumably have not configured a session context. Have a read of this.
 
Avinash Kandoti
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the configured session management still doesnt work.
here is the hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/in</property>
<property name="hibernate.connection.username">root</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<mapping resource="hobernatetest/Tbhhcfacility.hbm.xml"/>
<mapping resource="hobernatetest/Tbscfacility.hbm.xml"/>
<mapping resource="hobernatetest/PhpSession.hbm.xml"/>
<mapping resource="hobernatetest/Tbdcsfacility.hbm.xml"/>
<mapping resource="hobernatetest/Tbsbafacility.hbm.xml"/>
<mapping resource="hobernatetest/Tbsphefacility.hbm.xml"/>
<mapping resource="hobernatetest/Tbncpafacility.hbm.xml"/>
</session-factory>
</hibernate-configuration>

And my java code for the ejb

package hobernatetest;

import javax.ejb.Stateless;
import org.hibernate.Session;
import org.hibernate.Query;
import java.util.List;

@Stateless
public class EJBTest implements EJBTestRemote, EJBTestLocal {

Session session = null;

public EJBTest(){
this.session = NewHibernateUtil.getSessionFactory().getCurrentSession();
org.hibernate.Transaction tx = session.beginTransaction();

}

public List businessMethod() {
List<Tbhhcfacility> facility = null;
try {

Query q = session.createQuery ("from Tbhhcfacility as facility ");
facility = (List<Tbhhcfacility>) q.list();
} catch (Exception e) {
e.printStackTrace();
}
return facility;
}



}

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

@PersistenceContext(unitName = "YourPersistenceUnitGoesHere")
Session session = null;
 
Avinash Kandoti
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

that doesn't work either. Im actually new to EJBs Im using netbeans and hope I have done this stuff right. Here is the thing Im tryin to do USe hibernate for persistence. Use a stateless eJB to just run a from <tablename> to fetch all rowa. Use a servlet to invoke the EJB remote class and finally use a JSP to invoke the servlet. Here are My classes..

EJBTest.java
______________

package hobernatetest;

import javax.ejb.Stateless;
import org.hibernate.Session;
import org.hibernate.Query;
import java.util.List;
import javax.persistence.PersistenceContext;

@Stateless
public class EJBTest implements EJBTestRemote, EJBTestLocal {

@PersistenceContext(unitName = "hibernate.cfg.xml")
Session session = null;


public EJBTest(){
this.session = NewHibernateUtil.getSessionFactory().getCurrentSession();
org.hibernate.Transaction tx = session.beginTransaction();

}

public List businessMethod() {
List<Tbhhcfacility> facility = null;
try {

Query q = session.createQuery ("from Tbhhcfacility as facility ");
facility = (List<Tbhhcfacility>) q.list();
} catch (Exception e) {
e.printStackTrace();
}
return facility;
}

// Add business logic below. (Right-click in editor and choose
// "Insert Code > Add Business Method")

}

EJBTestRemote.java
_________________

package hobernatetest;

import java.util.List;
import javax.ejb.Remote;


@Remote
public interface EJBTestRemote {
List businessMethod();
}


TestServlet.java
______________

package hobernatetest;


import java.io.*;

import javax.ejb.EJB;
import javax.servlet.*;
import javax.servlet.http.*;


public class TestServlet extends HttpServlet {
@EJB
private EJBTestRemote testEJB;



@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet TestServlet</title>");
out.println("</head>");
out.println("<body>");
out.println(testEJB.businessMethod());
out.println("</body>");
out.println("</html>");
}

}


hibernate.cfg.xml
_________________

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/in</property>
<property name="hibernate.connection.username">root</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<mapping resource="hobernatetest/Tbhhcfacility.hbm.xml"/>
<mapping resource="hobernatetest/Tbscfacility.hbm.xml"/>
<mapping resource="hobernatetest/PhpSession.hbm.xml"/>
<mapping resource="hobernatetest/Tbdcsfacility.hbm.xml"/>
<mapping resource="hobernatetest/Tbsbafacility.hbm.xml"/>
<mapping resource="hobernatetest/Tbsphefacility.hbm.xml"/>
<mapping resource="hobernatetest/Tbncpafacility.hbm.xml"/>
</session-factory>
</hibernate-configuration>


The persistent classes I created and reveng.xml seem to work fine because I can run the HQL query in the IDE and it returns all the rows for the table.
Thanks for the help guys and any tutorials to just achive the same functionality would help im not real specific on just makin this work


 
Ranch Hand
Posts: 466
1
IntelliJ IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kindly format your code for more readability.
 
Avinash Kandoti
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hy sorry for the poor formatting anyways here goes...

Im actually new to EJBs Im using netbeans and hope I have done this stuff right. Here is the thing Im tryin to do USe hibernate for persistence. Use a stateless eJB to just run a from <tablename> to fetch all rowa. Use a servlet to invoke the EJB remote class and finally use a JSP to invoke the servlet. Here are My classes..

EJBTest.java
______________


EJBTestRemote.java
_________________



TestServlet.java
______________



hibernate.cfg.xml
_________________



The persistent classes I created and reveng.xml seem to work fine because I can run the HQL query in the IDE and it returns all the rows for the table.
Thanks for the help guys and any tutorials to just achive the same functionality would help im not real specific on just makin this work

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic