-->
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.  [ 5 posts ] 
Author Message
 Post subject: Integration Of Hibernate with EJB
PostPosted: Thu Apr 12, 2007 7:26 am 
Newbie

Joined: Wed Apr 11, 2007 8:22 am
Posts: 2
Hi,
I am developing an web base project.In my project i am calling hibernate with stateless session bean.
That is

stateless session bean calls hibernate then hibernate store data which i want to store in database.

So to do what should i do.I any one have sample code plz post it as early as possible.

Thanks in advance


Top
 Profile  
 
 Post subject: EJB / Hibernate Integration
PostPosted: Thu Apr 12, 2007 12:48 pm 
Newbie

Joined: Sat Dec 02, 2006 1:09 am
Posts: 12
I do not have an answer to your problem. I am new to Hibernate myself. I am just wondering about integrating Hibernate and EJB. I thought they were both trying to address the same problems and therefore competing products.

Is this a requirement from a so called "architect" or are you choosing to do this yourself?

pb


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 12, 2007 1:54 pm 
Beginner
Beginner

Joined: Thu Apr 12, 2007 3:26 am
Posts: 35
Location: Germany
Hi

Hibernate and EJB aren't adressing the same problem and therefore aren't competing products. EJB uses Java Persistence for Persistence purposes and Hibernate is an Java Persistence Provider.

I think there are severall ways to use Hibernate with EJB:
1) Use only the Java Persistence stuff. So let the application server inject an EntityManager into your Session Bean. Use this EntityManager to persist Entities. In this case Hibernate works in the background for you, so you aren't bound to Hibernate as persistence provider.
2)If you want to use the Hibernate Session instead of the EntityManager, you can get the Hibernate Session from the EntityManager. Do it this way: Session session = ((HibernateEntityManager)entityManager).getSession();
3)If you are using the application Server JBOSS, the Session can be injected directly into your bean by JBOSS.

Sorry that I haven't written down any code examples, but my book "Java persistence with hibernate" (one of my best books) is still at my company. And without the book (I'm very new to Hibernate and Java persistence) I wouldn't be sure that the code I wrote down is correct.

I hope my hint will help you
hoeft


Top
 Profile  
 
 Post subject: Integration Of Hibernate with EJB
PostPosted: Fri Apr 13, 2007 12:48 am 
Newbie

Joined: Wed Apr 11, 2007 8:22 am
Posts: 2
Thanks for quick response

I am new in hibernate tech. So i am not getting what u want to suggest. Can give me sample code if u have any.
I will be very thankful for it.
waiting for reply.

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 13, 2007 3:06 am 
Beginner
Beginner

Joined: Thu Apr 12, 2007 3:26 am
Posts: 35
Location: Germany
Hi!

To let your application server inject an EntityManager in your bean write:
Code:
@Stateless
public class MySimpleBean implements MySimpleBeanRemote {
    @PersistenceContext(unitName="the name of your persitence unit")
    private EntiyManager entityManager
   
    ...
}


If your are using jboss, the session can be injected directly:
Code:
@Stateless
public class MySimpleBean implements MySimpleBeanRemote {
    @PersistenceContext(unitName="the name of your persitence unit")
    Session session
   
    ...
}


If your aren't using JBOSS but you want to work with a session instead of an entiymanager do it right this:
Code:
@Stateless
public class MySimpleBean implements MySimpleBeanRemote {
    private Session session

    @PersistenceContext(unitName="the name of your persitence unit")
    public void setEntityManager() {
        session = ((HibernateEntityManager)entityManager).getSession();
    }
   
    ...
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.