-->
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.  [ 18 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: EJB CMP or Hibernate?????/
PostPosted: Mon Apr 05, 2004 10:59 pm 
Newbie

Joined: Thu Apr 01, 2004 6:28 pm
Posts: 15
Which one is best suited for large applications
I'm kind resistent to the idea that session beans using hibernate can run faster or more stable than just plain cmp, but maybe I'm biased
Anyone


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 06, 2004 12:30 am 
Regular
Regular

Joined: Fri Sep 05, 2003 12:01 am
Posts: 80
Location: Bogot
I dont really have any benchmarks to speak off comparing CMP to Hibernate.

One bad thing about CMP is the N+1 Db hits problem when you use finder methods to load a collection , a potential performance drag! In big apps you will normally find the DB to be your bottleneck, mainly cause its the hardest place to run in parallel.

One good thing about CMP is that you can have the App server run your app distributed.

IMHO Hibernate is much more flexible when it comes to handle fine grained models.

What do you mean by large? In storage? Transaction intensive? Is it web based? This are thing you have to consider

_________________
Mauricio Hern


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 06, 2004 12:48 am 
Beginner
Beginner

Joined: Thu Jan 08, 2004 4:40 am
Posts: 48
Location: Bangkok, Thailand
Hibernate for sure.
    Very fast to develop
    Very simple
    Very good performance
    Very good support(forum)
    Very good document and manual


Currently, we are using

Servlet
| ----call---->SessionBean
|----call------>DAO
|----call----->Hibernate

It's really cool....................

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


Top
 Profile  
 
 Post subject: Could you give a complete example ?
PostPosted: Tue Apr 06, 2004 9:34 am 
Newbie

Joined: Tue Apr 06, 2004 9:15 am
Posts: 4
Hi,

I'm begining to learn hibernate.
I like your solution. Could you give an example that I could see it in action.
Example: a zip with Servlet, SessionBean, DAO and Hibernate files of one simple code like simple reading and writing contacts to DB.

Thanks,
Paulo



roofimon wrote:
Hibernate for sure.
    Very fast to develop
    Very simple
    Very good performance
    Very good support(forum)
    Very good document and manual


Currently, we are using

Servlet
| ----call---->SessionBean
|----call------>DAO
|----call----->Hibernate

It's really cool....................


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 07, 2004 12:30 am 
Beginner
Beginner

Joined: Thu Jan 08, 2004 4:40 am
Posts: 48
Location: Bangkok, Thailand
OK, This is the beginning an Interface class

Code:
package com.netplus.workstation.pprincess;

import java.sql.Blob;

public interface Limousine {

   Long getLimousineId();
   void setLimousineId(Long limId);

   String getLimousineType();
   void setLimousineType(String limType);
   
   String getDescription();
   void setDescription(String description);
   
   Double getRate();
   void setRate(Double rate);
   
   Blob getPicture();
       void setPicture(Blob pic);
   
   String getFileName();
   void setFileName(String fileName);
   
   int getFileSize();
   void setFileSize(int fileSize);
}


And here is My Real Hibernate Obj that implement above Interface

Code:
package com.netplus.workstation.pprincess.hibernate.entity;

import com.netplus.workstation.pprincess.Limousine;
import java.sql.Blob;
import java.io.Serializable;
/**
* @castor.class
* @hibernate.class table="LIMOUSINE"
*/
public class HibernateLimousine implements Limousine , Serializable {

   private Long limousineId;
   private String limousineType;
   private String description;
   private Double rate;
   private Blob picture;
   private String fileName;
   private int fileSize;

   /**
    * @castor.field   
    *      type = "java.lang.Long"
    * @castor.field-xml
    *      name = "limousineId"
    *      node = "attribute"
    * @hibernate.id
    *  generator-class="assigned"
    *  unsaved-value="0"
    */
   public Long getLimousineId(){
      return limousineId;
   }
   public void setLimousineId(Long limousineId){
      this.limousineId = limousineId;
   }
   
   
   /**
    * @castor.field   
    *      type = "java.lang.String"
    * @castor.field-xml
    *      name = "limousineType"
    *      node = "element"
    * @hibernate.property
    */
   public String getLimousineType(){
      return limousineType;
   }
   public void setLimousineType(String limousineType){
      this.limousineType = limousineType;
   }
   
   /**
    * @castor.field   
    *      type = "java.lang.String"
    * @castor.field-xml
    *      name = "description"
    *      node = "element"
    * @hibernate.property
    */
   public String getDescription(){
      return description;
   }
   public void setDescription(String description){
      this.description = description;
   }
   
   /**
    * @castor.field   
    *      type = "java.lang.Double"
    * @castor.field-xml
    *      name = "rate"
    *      node = "element"
    * @hibernate.property
    */
   public Double getRate(){
      return rate;
   }
   public void setRate(Double rate){
      this.rate = rate;
   }
   
   /**
    * @castor.field   
    *      type = "java.sql.Blob"
    * @castor.field-xml
    *      name = "picture"
    *      node = "element"
    * @hibernate.property
    */
   public Blob getPicture(){
      return picture;
   }
       public void setPicture(Blob picture){
      this.picture = picture;
   }
   
   /**
    * @castor.field   
    *      type = "java.lang.String"
    * @castor.field-xml
    *      name = "fileName"
    *      node = "element"
    * @hibernate.property
    */
   public String getFileName(){
      return fileName;
   }
   public void setFileName(String fileName){
      this.fileName = fileName;
   }
   
   /**
    * @castor.field   
    *      type = "integer"
    * @castor.field-xml
    *      name = "fileSize"
    *      node = "element"
    * @hibernate.property
    */
   public int getFileSize(){
      return fileSize;
   }
   public void setFileSize(int fileSize){
      this.fileSize = fileSize;
   }
}

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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 07, 2004 12:34 am 
Beginner
Beginner

Joined: Thu Jan 08, 2004 4:40 am
Posts: 48
Location: Bangkok, Thailand
Again this is my Interface FOR DAO

Code:
package com.netplus.workstation.pprincess.hibernate.dao;

import java.util.List;
import com.netplus.workstation.pprincess.Limousine;
import net.sf.hibernate.HibernateException;

public interface HibernateLimousineDAO {
       public Limousine create(String limType,String description,Double rate,byte[] pic,String fileName,int fileSize) throws Exception;
       public List findByAll() throws Exception;
       public Limousine findByPrimaryKey(long limId) throws Exception;
       public void init() throws HibernateException;
       public void remove(long limId) throws Exception;
       public void update(Limousine limousine) throws Exception;
}


And this is my Real DAO
Code:
package com.netplus.workstation.pprincess.hibernate.impl;

import java.util.List;
import java.io.Serializable;
import javax.ejb.EJBException;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.InitialContext;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Session;
import net.sf.hibernate.Hibernate;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.Query;
import com.netplus.workstation.pprincess.Limousine;
import com.netplus.workstation.pprincess.hibernate.dao.HibernateLimousineDAO;
import com.netplus.workstation.pprincess.hibernate.entity.HibernateLimousine;


public class HibernateLimousineDAOImpl implements HibernateLimousineDAO , Serializable {
   
    private Session hbSession;
    private SessionFactory factory;

   
    public HibernateLimousineDAOImpl() {
    }
   
   
    public Limousine create(String limType, String description, Double rate, byte[] pic, String fileName, int fileSize) throws Exception{
        try{
            System.out.println("Create Limousine.");
            hbSession = factory.openSession();
            Transaction tx = hbSession.beginTransaction();
       HibernateLimousine hLimousine = new HibernateLimousine();
       hLimousine.setLimousineType(limType);
       hLimousine.setDescription(description);
       hLimousine.setRate(rate);
       hLimousine.setPicture(Hibernate.createBlob(pic));
       hLimousine.setFileName(fileName);
       hLimousine.setFileSize(fileSize);
       long id;
            try{
          id = ((Long)(hbSession.iterate("select max(limousine.limousineId) from com.netplus.workstation.pprincess.hibernate.entity.HibernateLimousine limousine").next())).longValue()+1;
       } catch(Exception ex){
          id = 1;
       }
            Long genId = new Long(id);
            hbSession.save(hLimousine, genId);
            tx.commit();
            return (Limousine)hbSession.load(HibernateLimousine.class, genId);
        }catch(HibernateException error){
            throw new Exception("Error saving object: "+ error.toString());
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }finally{
            hbSession.close();
        }
    }
   
   
    public List findByAll() throws Exception {
   try{
            System.out.println("Find Limousine All.");
            hbSession = factory.openSession();
            Transaction tx = hbSession.beginTransaction();
       Query query = hbSession.createQuery("from com.netplus.workstation.pprincess.hibernate.entity.HibernateLimousine");
       List list = query.list();
            System.out.println("Get Limousine size? "+list.size());
       return list;
        }catch(HibernateException error){
            throw new Exception("Error FindByAll = "+error.toString());
        }catch(Exception error){
            error.printStackTrace();
            return null;
        }finally{
            hbSession.close();
        }
    }
   
    public Limousine findByPrimaryKey(long limId) throws Exception{
        try{
            System.out.println("Find By Limousine Id.");
            hbSession = factory.openSession();
            Transaction tx = hbSession.beginTransaction();
            HibernateLimousine hLimousine = (HibernateLimousine)hbSession.load(HibernateLimousine.class, new Long(limId));
       return (Limousine)hLimousine;
        }catch(HibernateException error){
            throw new Exception("Error find by limousine id = "+ limId +" . "+error.toString());
        }catch(Exception error){
            error.printStackTrace();
            return null;
        }finally{
            hbSession.close();
        }
    }
   
    public void init() throws HibernateException {
        try {
            System.out.println("Init DAO");
            Context ctx = new InitialContext();
           try {
                    factory = (SessionFactory) ctx.lookup("java:/PPrincessHibernateFactory");
                } catch (NamingException e) {
                    throw new EJBException("Error looking up dataSource: " + e.toString());
                }
   } catch (NamingException e) {
      throw new EJBException("Error initializing context:" + e.toString());
   }
    }
   
   
    public void remove(long limId) throws HibernateException {
        try{
            System.out.println("Remove Limousine.");
            hbSession = factory.openSession();
            Transaction tx = hbSession.beginTransaction();
            HibernateLimousine hLimousine = (HibernateLimousine)hbSession.load(HibernateLimousine.class, new Long(limId));
            hbSession.delete(hLimousine);
            tx.commit();
        }catch(HibernateException error){
           
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            hbSession.close();
        }
    }
   
    public void update(Limousine limousine) throws Exception{
        try{
            System.out.println("Update Limousine.");
            hbSession = factory.openSession();
            Transaction tx = hbSession.beginTransaction();
            hbSession.saveOrUpdate(limousine);
            tx.commit();
        }catch(HibernateException error){
            throw new Exception("Error updating object: "+ error.toString());
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            hbSession.close();
        }
    }

}
[/code]

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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 07, 2004 12:37 am 
Beginner
Beginner

Joined: Thu Jan 08, 2004 4:40 am
Posts: 48
Location: Bangkok, Thailand
Here is my Session Bean

Code:
package com.netplus.workstation.pprincess.ejb;

import com.netplus.workstation.pprincess.hibernate.dao.HibernateLimousineDAO;
import com.netplus.workstation.pprincess.hibernate.entity.HibernateLimousine;
import com.netplus.workstation.pprincess.Limousine;
import net.sf.hibernate.HibernateException;
import java.util.List;
/**
*
* @ejb.bean
*    name="Limousine"
*    type="Stateful"
*    view-type="remote"
*    jndi-name="hibernate/com.netplus.workstation.pprincess.ejb.Limousine"
*
* @ejb.home
*    remote-class="com.netplus.workstation.pprincess.ejb.LimousineHome"
*    extends = "javax.ejb.EJBHome"
*
* @ejb.interface
*    remote-class="com.netplus.workstation.pprincess.ejb.LimousineRemote"
*    extends="javax.ejb.EJBObject"
*
* @ejb.env-entry
*    name="limousineDao"
*    type="java.lang.String"
*    value="com.netplus.workstation.pprincess.hibernate.impl.HibernateLimousineDAOImpl"
*
* @ejb.util
*   generate = "physical"
*
* @jboss.web-xml
*   ref-name = "Limousine"
*   jndi-name = "hibernate/com.netplus.workstation.pprincess.ejb.Limousine"
*
* @web.ejb-ref
*   name = "Limousine"
*   type = "Session"
*   home = "com.netplus.workstation.pprincess.ejb.LimousineHome"
*   remote = "com.netplus.workstation.pprincess.ejb.LimousineRemote"
*
*/
public class LimousineFacadeEJB implements javax.ejb.SessionBean {

   private HibernateLimousineDAO limousineDao;

   public LimousineFacadeEJB() {
   }

   public void ejbCreate() {

   }
   public void ejbActivate() {
   }

   public void ejbPassivate() {
   }

   public void ejbRemove() {
   }

   public void setSessionContext(javax.ejb.SessionContext sessionContext) {
   }

   /**
   *@ejb.interface-method
   **/
   public Limousine createLimousine(String limType, String description, Double rate, byte[] pic, String fileName, int fileSize){
      try{
         return this.getDAO().create(limType, description, rate, pic, fileName, fileSize);
      }catch(Exception error){
         error.printStackTrace();
         return null;
      }
   }

   /**
    * @ejb.interface-method
    */
   public List getAllLimousines() {
      try {
         return this.getDAO().findByAll();
      } catch (Exception error) {
         error.printStackTrace();
         return null;
      }
   }
   
   /**
    * @ejb.interface-method
    */
   public Limousine getLimousineInstance(){
      return (Limousine) (new HibernateLimousine());
   }
   
   /**
    * @ejb.interface-method
    */
   public Limousine findByPrimaryKey(long limId) {
      try {
         return (Limousine)this.getDAO().findByPrimaryKey(limId);
      } catch (Exception error) {
         error.printStackTrace();
         return null;
      }
   }


   /**
    * @ejb.interface-method
    */
   public void removeLimousine(long limId) {
      try {
         this.getDAO().remove(limId);
      } catch (Exception error) {
         error.printStackTrace();
      }
   }

   /**
    * @ejb.interface-method
    */
   public void updateLimousine(Limousine limousine) {
      try {
         this.getDAO().update(limousine);
      } catch (Exception error) {
         error.printStackTrace();
      }
   }

   public HibernateLimousineDAO getDAO() throws HibernateException {
      if (limousineDao != null) {
         return limousineDao;
      } else {

         try {
            javax.naming.InitialContext ctx = new javax.naming.InitialContext();
            Object ref = ctx.lookup("java:comp/env/limousineDao");
            String daoStr = (String) javax.rmi.PortableRemoteObject.narrow(ref, String.class);
            limousineDao = (HibernateLimousineDAO) Class.forName(daoStr).newInstance();
         } catch (javax.naming.NamingException e) {
            throw new IllegalStateException(
               "DAO not defined in JNDI 'java:comp/env/limousineDao': " + e.getLocalizedMessage());
         } catch (Exception e) {
            throw new IllegalStateException(
               "Exception while looking in JNDI for 'java:comp/env/limousineDao': " + e.getLocalizedMessage());
         }
         System.out.println("Going to Init");
         limousineDao.init();
         return limousineDao;
      }
   }
}

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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 07, 2004 12:40 am 
Beginner
Beginner

Joined: Thu Jan 08, 2004 4:40 am
Posts: 48
Location: Bangkok, Thailand
Lastly, This is my Servlet. Please, make any comments on my coding style.

Code:
package com.netplus.workstation.pprincess.module.admin;

import com.netplus.cocoon.struts.AbstractModule;
import com.netplus.cocoon.struts.ActionForm;
import com.netplus.cocoon.struts.ActionForward;
import com.netplus.cocoon.struts.ActionMapping;

import com.netplus.workstation.pprincess.Limousine;
import com.netplus.workstation.pprincess.ejb.LimousineHome;
import com.netplus.workstation.pprincess.ejb.LimousineRemote;
import com.netplus.workstation.pprincess.ejb.LimousineUtil;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.avalon.framework.parameters.Parameters;

public final class LimousineInfo extends AbstractModule {

   public ActionForward perform(ActionMapping mapping, ActionForm form,
                                ServletContext scontext, HttpServletRequest request,
                                HttpServletResponse response, Parameters par)
       throws ServletException {

      LimousineHome limousineHome = null;
      LimousineRemote session = null;
      
      long limId = Long.parseLong(request.getParameter("limousineId"));
      try {
         System.out.println("LimousineInfo was executed");
         limousineHome = LimousineUtil.getHome();
         session = limousineHome.create();
         Limousine limousine = session.findByPrimaryKey(limId);
         request.setAttribute("limousineInfo", limousine);
         return (mapping.findForward("success"));
      } catch(Exception e) {
         getLogger().error(e.toString(), e);
         throw new ServletException("Error performing : " + e.getMessage());
      } finally {

      }
   }
}

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


Top
 Profile  
 
 Post subject: Thanks Arin
PostPosted: Wed Apr 07, 2004 9:27 am 
Newbie

Joined: Tue Apr 06, 2004 9:15 am
Posts: 4
Thanks Arin,

It will be a good help for me. I'm just beggining and every simple help will be a great help for me.
But, just a little question more.
Why you use @castor doclet in DAO Impl ?


Paulo


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 08, 2004 1:13 am 
Beginner
Beginner

Joined: Thu Jan 08, 2004 4:40 am
Posts: 48
Location: Bangkok, Thailand
Dear Paulo
I couldn't find @castor in DAO implement, but it's in my Hibernate Implement......Actually, I use castor tag just to transform my obj to XML at presentation layer and we used CASTOR transformer.

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 09, 2004 4:40 pm 
Newbie

Joined: Thu Apr 01, 2004 6:28 pm
Posts: 15
Why not use hibernate with a Pojo, why is waste time following RIDICOULOUS standards if that doesnt make you money????/
THATS my question


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 12, 2004 11:36 pm 
Beginner
Beginner

Joined: Thu Jan 08, 2004 4:40 am
Posts: 48
Location: Bangkok, Thailand
Calm down buddy
Yeah, This standard is quite RIDICOULOUS, however not all of it is bad. Our reason to did this because we was working with Entity(BMP)+Session and we found that BMP cost us lot of time to develop and it's not that simple as well.
We decide to changed but as you know (I though) when people get use to something it is very hard to rapidly change it. "Do a little step" is good to follow. So I did change from BMP -> Hibernate first and it would have some more later on.

Lastly, pardon me my English
Roofimon

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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 13, 2004 2:40 am 
Beginner
Beginner

Joined: Tue Mar 16, 2004 5:15 am
Posts: 33
@roofimon

Take a look at jakarta.apache.org -> commons/logging and log4j. this println is a trouble some approach.

Other more complex hibernate examples are bundled with the spring framework distribution. Check those out! :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 13, 2004 8:36 am 
Beginner
Beginner

Joined: Thu Jan 08, 2004 4:40 am
Posts: 48
Location: Bangkok, Thailand
thx
Yeah, I'm reading on both Spring and Logging Framework.

Cheers
Roofimon

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


Top
 Profile  
 
 Post subject: Exactly what I was looking for...
PostPosted: Sat May 08, 2004 7:18 pm 
Beginner
Beginner

Joined: Thu Apr 29, 2004 12:45 pm
Posts: 45
arin,

Thanks for the great example! This is a really big help for developing my project. Your code is easy to follow and organized nicely. I notice that methods in the DAO and Session classes return objects through the Limousine interface. Does this increase performance?

I mean, you could alternatively instantiate DAOImpl from Session and call its methods without returning DAOImpl, via the Limousine interface.

I'm not an expert, so I'd greatly appreciate your comments.

Regards,
Itchy


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 18 posts ]  Go to page 1, 2  Next

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.