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.  [ 4 posts ] 
Author Message
 Post subject: hibernate pojos and domain model design question
PostPosted: Fri Sep 28, 2007 10:00 am 
Beginner
Beginner

Joined: Wed Feb 14, 2007 10:43 am
Posts: 21
Hibernate pojos map to database tables. Does it make sense to put logic in these pojos? Since these are mostly generated objects, shouldn't logic be in a different set of business objects? If so, isn't copying the hibernate pojos back and forth redundant activity? On the other hand, if we put too much logic in hibernate pojos, don't they become heavy-weight? Don't they start smelling like Entity Beans? Would like to hear from Hibernate experts ....

Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html


Top
 Profile  
 
 Post subject: Re: hibernate pojos and domain model design question
PostPosted: Sat Sep 29, 2007 12:51 am 
Beginner
Beginner

Joined: Sat May 12, 2007 2:55 am
Posts: 24
For hibernate one of the standard way is to layout a facade lookup design pattern, which delegates the objects, in which your pojo(Models) do only have getter-setters, all your business logic goes to DataAccessObjects and common logic goes to parent-abstract DataAccessObjects. in this way your in-out of pojo will be very fast and your code will also be easily managable, extendsible.



hope this helps,

dont forget to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 29, 2007 8:11 am 
Beginner
Beginner

Joined: Wed Feb 14, 2007 10:43 am
Posts: 21
Can you pls point me to any details samples of this pattern?


Top
 Profile  
 
 Post subject: Pattern is like this:
PostPosted: Sun Sep 30, 2007 11:16 pm 
Newbie

Joined: Fri Aug 31, 2007 11:58 am
Posts: 19
Pojo is Employee
Code:
/**
*
*/
package com.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

/**
* @author singhk
*
*/
@Entity
@Table(name = "EMPLOYEE")
@NamedQueries( { @NamedQuery(name = "findEmpByDept", query = "SELECT i FROM Employee i WHERE i.department=:department"), })
@SequenceGenerator(initialValue = 1, name = "employeeIdSeq", sequenceName = "EMPLOYEE_ID_SEQ")
public class Employee implements Serializable {
   private static final long serialVersionUID = 1L;

   @Id
   @GeneratedValue(generator = "employeeIdSeq", strategy = GenerationType.SEQUENCE)
   private Long id;
   @Column(name = "name", nullable = false)
   private String name;
   @Column(name = "department", nullable = false)
   private String department;

   public Employee() {
      super();
   }

   public Employee(String name, String department) {
      super();
      this.name = name;
      this.department = department;
   }

   public Long getId() {
      return id;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public String getDepartment() {
      return department;
   }

   public void setDepartment(String department) {
      this.department = department;
   }

}



And this is its DAO:
Code:
/**
*
*/
package com.model.daoImpl;

import java.util.List;

import com.model.Employee;
import com.model.dao.EmployeeDAO;

/**
* @author singhk
*
*/
public class EmployeeDAOImpl extends GenericDAOImpl<Employee, Long> implements
      EmployeeDAO {

   @SuppressWarnings("unchecked")
   public List<Employee> findEmpByDept(String department) {
      return getEntityManager().createNamedQuery("findEmpByDept")
            .setParameter("department", department).getResultList();
   }
}

And this is what is meant by :common logic goes to parent-abstract DataAccessObjects
Code:
package com.model.daoImpl;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.model.dao.GenericDAO;

public abstract class GenericDAOImpl<T, ID extends Serializable> implements GenericDAO<T,ID>
{
   private Class<T> entityBeanType;
   private EntityManager em;

   @SuppressWarnings("unchecked")
   public GenericDAOImpl()
   {
     this.entityBeanType = (Class<T>) ( (ParameterizedType) getClass().getGenericSuperclass() ).getActualTypeArguments()[0];
   }

   @PersistenceContext
   public void setEntityManager(EntityManager em)
   {
      this.em = em;
   }
   
   public EntityManager getEntityManager()
   {
      return em;
   }

   public T findById(ID id)
   {
      T entity;
   
      
         entity = getEntityManager().find( getEntityBeanType(), id );
      
      
      return entity;
   }

   @SuppressWarnings("unchecked")
   public List<T> findAll()
   {
      List<T> allEntities = getEntityManager().createQuery( "from " + getEntityBeanType().getName() ).getResultList();
      return allEntities;
   }
   
   public void clear()
   {
      getEntityManager().clear();
   }
   
   
   protected Class<T> getEntityBeanType()
   {
      return entityBeanType;
   }
}


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