This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EJB with Hibernate

 
Avinash Kandoti
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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
|#]



 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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
  • 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;
}



}

 
Christian Dillinger
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try

@PersistenceContext(unitName = "YourPersistenceUnitGoesHere")
Session session = null;
 
Avinash Kandoti
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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


 
Vinod Tiwari
Ranch Hand
Posts: 466
1
IntelliJ IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kindly format your code for more readability.
 
Avinash Kandoti
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic