-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 14 posts ] 
Author Message
 Post subject: how hibernate work with ejb?
PostPosted: Thu Mar 04, 2004 2:17 am 
Beginner
Beginner

Joined: Fri Feb 27, 2004 3:49 am
Posts: 25
i am beginner of hibernate. i need to develop a 3-tiers application using ejb. how intergate hibernate into ejb(session bean)? hava any complete and clear reference or example code for it? thanks ...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 04, 2004 3:10 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
It is common for hibernate to be used from session bean(s) and work with CMT. There are examples in the examples section and lost of discussion in the forum as you will find by searching.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 04, 2004 4:34 am 
Beginner
Beginner

Joined: Thu Jan 08, 2004 4:40 am
Posts: 48
Location: Bangkok, Thailand
For me I have read this and it give me an idea how to use HB with Session.
[url]
http://www.hibernate.org/82.html[/url]

Try you will love HB
All the best

_________________
<name>arin</name>
<at>netplus software</at>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 04, 2004 10:29 pm 
Beginner
Beginner

Joined: Fri Feb 27, 2004 3:49 am
Posts: 25
after do reference, i write a session bean like below:

public class MySessionBean implements SessionBean{
private SessionContext ctx;
private Session session;
private Transaction tx;
private SessionFactory factory;

private Log logger = LogFactory.getLog(InventoryBean.class);

public void setSessionContext(SessionContext ctx) throws EJBException,
RemoteException {
this.ctx = ctx;
}

public SessionContext getSessionContext(){
return this.ctx;
}

public void ejbCreate(){
if(logger.isInfoEnabled()){
logger.info("Session bean created");
}
Context context = null;
try{
context = new InitialContext();
factory = (SessionFactory)context.lookup
("java:/hibernate/HibernateFactory");
}catch(NamingException e){}
}


public void ejbRemove() throws EJBException, RemoteException {
if(logger.isInfoEnabled()){
logger.info("Session bean removed");
}
ctx = null;
}

public void ejbActivate() throws EJBException, RemoteException {
}

public void ejbPassivate() throws EJBException, RemoteException {
}

/**
* @ejb.interface-method
*
* @param site
* @param document
* @return
* @throws ServiceException
*/
public Inventory createDoc(Site site, Inventory document) throws ServiceException, HibernateException{
HibernateSession hs = new HibernateSession);
Session session = hs.currentSession(site);

Transaction tx = null;
try{
tx = session.beginTransaction();

session.save(document);
tx.commit();
}catch(HibernateException e){
if (tx!=null)
tx.rollback();
logger.debug("HibernateException: " +e.getMessage());
throw new ServiceException("Failed to create document");
}finally{
session.close();
}

return document;
}
}


HibernateSession.java
public class HibernateSession {
static Log logger = LogFactory.getLog(HibernateSession.class);
static Configuration cfg = null;
static SessionFactory sf = null;
public static final ThreadLocal local = new ThreadLocal();

static {
try {
init();
} catch (Exception e) {
logger.error("could not init " + e.getMessage());
}
}

public HibernateSession() {
super();
}

public Session currentSession(Site site) throws ServiceException {
Session session = (Session) local.get();
if (session == null) {
Connection con = null;

if (sf == null) {
init();
}

try{
String jndiName = site.getMbeanJndiName();
DataSource ds = (DataSource)NamingHelper.getInitialContext(new Properties()).lookup(jndiName);
con = ds.getConnection();

session = sf.openSession(con);
}catch(SQLException e){
logger.debug("SQLException: " + e.getMessage());
throw new ServiceException("Failed to create datasource.");
}catch(NamingException e){
logger.debug("NamingException: " + e.getMessage());
throw new ServiceException("Failed to look up JndiName:" + site.getMbeanJndiName());
}

local.set(session);
}
return session;
}

public Session currentSession() throws ServiceException{
Session session = (Session) local.get();
if (session == null) {
if (sf == null) {
init();
}

try{
session = sf.openSession();
}catch(HibernateException e){
logger.debug("HibernateException: " + e.getMessage());
throw new ServiceException("Failed to open a session from session factory.");
}

local.set(session);
}

return session;
}


public static String closeSession()throws HibernateException, SQLException {
Connection con = null;

Session session = (Session) local.get();

String sessionHashCode = session.toString();

local.set(null);
if (session != null) {
con = session.close();
}

if (con != null) {
con.close();
}

return sessionHashCode;
}

private static synchronized void init() throws ServiceException {
if (sf != null) {
return;
}
cfg = null;
try {
cfg =new Configuration().addClass(Inventory.class);

sf = cfg.buildSessionFactory();
logger.info("HibernateSession Initialized SessionFactory: " + sf);
} catch (Exception e) {
logger.error("Could not intialize Hibernate session factory. "
+ e.getMessage());
throw new ServiceException("Could not intialize Hibernate session factory. " + e.getMessage());
}
}

}

after run it, data is successfully write into database, but i got some error message.

i not sure my coding has problem or no. can me give some idea?

thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 04, 2004 10:55 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
What was the error message?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2004 1:23 am 
Beginner
Beginner

Joined: Fri Feb 27, 2004 3:49 am
Posts: 25
the error message...
...
2004-03-05 13:22:33,298 ERROR [STDERR] Query "insert into StockMaster (CategoryId, Description, LongDescription, Units, MBflag, LastCurCostDate, LastCost, ActualCost, MaterialCost, LabourCost, OverheadCost, LowestLevel, Discontinued, Controlled, EOQ, Volume, KGS, BarCode, StockId) values ('df', 'df', 'df', 'sd', 'M', null, 0, 0, 0, 0, 0, 2, 0, 0, 0.0, 2.0, 2.0, '', 'fdf')" execution time: 78
2004-03-05 13:22:33,298 DEBUG [net.sf.hibernate.impl.BatcherImpl] done closing: 0 open PreparedStatements, 0 open ResultSets
2004-03-05 13:22:33,298 DEBUG [net.sf.hibernate.impl.BatcherImpl] closing statement
2004-03-05 13:22:33,298 DEBUG [net.sf.hibernate.impl.SessionImpl] post flush
2004-03-05 13:22:33,298 ERROR [net.sf.hibernate.transaction.JDBCTransaction] Commit failed
java.sql.SQLException: You cannot commit during a managed transaction!
at org.jboss.resource.adapter.jdbc.BaseWrapperManagedConnection.jdbcCommit(BaseWrapperManagedConnection.java:495)
...

2004-03-05 13:22:33,313 DEBUG [net.sf.hibernate.impl.SessionImpl] transaction completion
2004-03-05 13:22:33,345 DEBUG [net.sf.hibernate.transaction.JDBCTransaction] rollback
2004-03-05 13:22:33,345 DEBUG [com.creativesol.erp4all.service.ejb.InventoryBean] HibernateException: Commit failed with SQL exception:
2004-03-05 13:22:33,345 DEBUG [net.sf.hibernate.impl.SessionImpl] closing session
2004-03-05 13:22:33,345 DEBUG [net.sf.hibernate.impl.SessionImpl] disconnecting session
2004-03-05 13:22:33,345 DEBUG [net.sf.hibernate.impl.SessionImpl] transaction completion
2004-03-05 13:22:33,360 INFO [org.jboss.resource.connectionmanager.CachedConnectionManager] Closing a connection for you. Please close them yourself: org.jboss.resource.adapter.jdbc.WrappedConnection@1a82e92
java.lang.Exception: STACKTRACE
....


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2004 4:11 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Your hibernate config file should be wrong. Check at least
jta.UserTransaction, hibernate.transaction.manager_lookup_class, hibernate.transaction.factory_class[/list]

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2004 4:43 am 
Beginner
Beginner

Joined: Fri Feb 27, 2004 3:49 am
Posts: 25
thanks for your reply.

i try to generate hibernate.cfg.xml then directly package it into my application war.

it is my hibernate.cfg.xml ...
<hibernate-configuration>
<session-factory name="java:comp/env/hibernate/SessionFactory">
<property name="connection.datasource">java:/MySqlDSERP4ALL</property>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">false</property>
<property name="use_outer_join">true</property>
<property name="jndi.name">java:/HibernateFactory</property>
<property name="transaction.factory_class">net.sf.hibernate.transaction.JTATransactionFactory</property>
<property name="transaction.manager_lookup_class">net.sf.hibernate.transaction.JBossTransactionManagerLookup</property>
<property name="jta.UserTransaction">UserTransaction</property>

<mapping resource="com.creativesol.erp4all.model/Inventory.hbm.xml"/>

</session-factory>
</hibernate-configuration>

is that wrong? i got a error message when my application server(jboss) redeploy application war. i ignore that error message because the application still can run until i add in my session bean (as i post before) and got problem mentioned before.

error message ...
2004-03-05 13:21:58,188 ERROR [org.jboss.deployment.scanner.URLDeploymentScanner] MBeanException: Exception in MBean operation 'checkIncompleteDeployments()'
Cause: Incomplete Deployment listing:
Packages waiting for a deployer:
<none>
Incompletely deployed packages:
<none>
MBeans waiting for classes:
<none>
MBeans waiting for other MBeans:
[ObjectName: jboss.jca:service=Hibernate
state: CONFIGURED
I Depend On: jboss.jca:service=RARDeployer
jboss.jca:service=LocalTxCM,name=mysql-erp4all-ds"

Depends On Me: ]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2004 9:11 pm 
Beginner
Beginner

Joined: Fri Feb 27, 2004 3:49 am
Posts: 25
first, i want thanks for your advance.

try and do some modification of my hibernate.cfg.xml and jbossService.xml. the big problem was solved.

but ...

jboss still display a error message after run the applicatioin. please help me!

error message ...
2004-03-06 09:05:47,539 DEBUG [net.sf.hibernate.impl.BatcherImpl] done closing: 0 open PreparedStatements, 0 open ResultSets
2004-03-06 09:05:47,539 DEBUG [net.sf.hibernate.impl.BatcherImpl] closing statement
2004-03-06 09:05:47,539 DEBUG [net.sf.hibernate.impl.SessionImpl] post flush
2004-03-06 09:05:47,539 DEBUG [net.sf.hibernate.impl.SessionImpl] closing session
2004-03-06 09:05:47,539 DEBUG [net.sf.hibernate.impl.SessionImpl] disconnecting session
2004-03-06 09:05:47,539 DEBUG [net.sf.hibernate.engine.CacheSynchronization] transaction before completion callback
2004-03-06 09:05:47,555 INFO [org.jboss.resource.connectionmanager.CachedConnectionManager] Closing a connection for you. Please close them yourself: org.jboss.resource.adapter.jdbc.WrappedConnection@b37089
java.lang.Exception: STACKTRACE
at org.jboss.resource.connectionmanager.CachedConnectionManager.registerConnection(CachedConnectionManager.java:282)
at org.jboss.resource.connectionmanager.BaseConnectionManager2.allocateConnection(BaseConnectionManager2.java:506)
at org.jboss.resource.connectionmanager.BaseConnectionManager2$ConnectionManagerProxy.allocateConnection(BaseConnectionManager2.java:814)
at org.jboss.resource.adapter.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:102)
at com.creativesol.erp4all.hibernate.HibernateSession.currentSession(HibernateSession.java:72)
at com.creativesol.erp4all.service.ejb.InventoryBean.createDoc(InventoryBean.java:125)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.jboss.ejb.StatelessSessionContainer$ContainerInterceptor.invoke(StatelessSessionContainer.java:683)
at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:185)
at org.jboss.ejb.plugins.StatelessSessionInstanceInterceptor.invoke(StatelessSessionInstanceInterceptor.java:72)
at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:84)
at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:267)
at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:128)
at org.jboss.ejb.plugins.SecurityInterceptor.invoke(SecurityInterceptor.java:118)
at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:191)
at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invoke(ProxyFactoryFinderInterceptor.java:122)
at org.jboss.ejb.StatelessSessionContainer.internalInvoke(StatelessSessionContainer.java:331)
at org.jboss.ejb.Container.invoke(Container.java:700)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.jboss.mx.capability.ReflectedMBeanDispatcher.invoke(ReflectedMBeanDispatcher.java:284)
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:546)
at org.jboss.invocation.local.LocalInvoker.invoke(LocalInvoker.java:101)
at org.jboss.invocation.InvokerInterceptor.invoke(InvokerInterceptor.java:90)
at org.jboss.proxy.TransactionInterceptor.invoke(TransactionInterceptor.java:46)
at org.jboss.proxy.SecurityInterceptor.invoke(SecurityInterceptor.java:45)
at org.jboss.proxy.ejb.StatelessSessionInterceptor.invoke(StatelessSessionInterceptor.java:100)
at org.jboss.proxy.ClientContainer.invoke(ClientContainer.java:85)
at $Proxy50.createDoc(Unknown Source)
at com.creativesol.erp4all.ServiceDelegate.InventoryService.createDoc(InventoryService.java:111)
at com.creativesol.erp4all.webAction.InventoryAction.addItem(InventoryAction.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:280)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:216)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.jboss.web.tomcat.security.JBossSecurityMgrRealm.invoke(JBossSecurityMgrRealm.java:220)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.CertificatesValve.invoke(CertificatesValve.java:246)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.jboss.web.tomcat.tc4.statistics.ContainerStatsValve.invoke(ContainerStatsValve.java:76)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2417)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.jboss.web.tomcat.tc4.authenticator.SingleSignOn.invoke(SingleSignOn.java:409)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:65)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:577)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:197)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:781)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:549)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:605)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:677)
at java.lang.Thread.run(Thread.java:534)
2004-03-06 09:05:47,570 ERROR [STDERR] Query "commit" execution time: 0


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 05, 2004 10:30 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Where do you call HibernateSession.closeSession() ?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 07, 2004 10:18 pm 
Beginner
Beginner

Joined: Fri Feb 27, 2004 3:49 am
Posts: 25
oh, i forgot to call this method.

i called it after my transaction and any thing can be run properly.
but i still got a error message while restart jboss server.

the error message ...
2004-03-08 10:07:06,616 ERROR [org.jboss.deployment.scanner.URLDeploymentScanner] MBeanException: Exception in MBean operation 'checkIncompleteDeployments()'
Cause: Incomplete Deployment listing:
Packages waiting for a deployer:
<none>
Incompletely deployed packages:
<none>
MBeans waiting for classes:
<none>
MBeans waiting for other MBeans:
[ObjectName: jboss.jca:service=HibernateFactory, name=hibernate/HibernateFactory
state: CONFIGURED
I Depend On: jboss.jca:service=RARDeployer
jboss.jca:service=LocalTxCM,name=mysql-erp4all-ds

Depends On Me: ]

how to fix it?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 08, 2004 12:45 pm 
Senior
Senior

Joined: Tue Nov 25, 2003 9:35 am
Posts: 194
Location: San Francisco
The Hibernate MBean did not deploy when you started up JBoss. There will be errors in the JBoss log - look at those and if you don't understand what is happening, post what you see.

Sherman


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 5:10 am 
Beginner
Beginner

Joined: Fri Feb 27, 2004 3:49 am
Posts: 25
what i can do next?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 5:30 am 
Newbie

Joined: Thu Feb 26, 2004 10:19 am
Posts: 2
Location: Berlin
tingling wrote:
what i can do next?


send again your code snipplet ...

think i can help


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 14 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.