Last active
May 7, 2016 23:46
-
-
Save bytestree/aa7fdeb89cdf55a11cff5b1228dd2c65 to your computer and use it in GitHub Desktop.
Generic DAO in Hibernate - EmployeeDao.java using GeneticDao.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bytestree.model; | |
import javax.persistence.Column; | |
import javax.persistence.Entity; | |
import javax.persistence.GeneratedValue; | |
import javax.persistence.GenerationType; | |
import javax.persistence.Id; | |
import javax.persistence.Table; | |
@Entity | |
@Table(name = "employee") | |
public class Employee implements java.io.Serializable { | |
private static final long serialVersionUID = 4910225916550731446L; | |
private Long id; | |
private String firstname; | |
private String lastname; | |
private String designation; | |
private Integer salary; | |
public Employee() { | |
} | |
public Employee(Long id) { | |
this.id = id; | |
} | |
public Employee(Long id, String firstname, String lastname, String designation, Integer salary) { | |
this.id = id; | |
this.firstname = firstname; | |
this.lastname = lastname; | |
this.designation = designation; | |
this.salary = salary; | |
} | |
public Employee(String firstname, String lastname, String designation, Integer salary) { | |
this.firstname = firstname; | |
this.lastname = lastname; | |
this.designation = designation; | |
this.salary = salary; | |
} | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
@Column(name = "id", unique = true, nullable = false) | |
public Long getId() { | |
return this.id; | |
} | |
public void setId(Long id) { | |
this.id = id; | |
} | |
@Column(name = "firstname", length = 50) | |
public String getFirstname() { | |
return this.firstname; | |
} | |
public void setFirstname(String firstname) { | |
this.firstname = firstname; | |
} | |
@Column(name = "lastname", length = 50) | |
public String getLastname() { | |
return this.lastname; | |
} | |
public void setLastname(String lastname) { | |
this.lastname = lastname; | |
} | |
@Column(name = "designation", length = 50) | |
public String getDesignation() { | |
return this.designation; | |
} | |
public void setDesignation(String designation) { | |
this.designation = designation; | |
} | |
@Column(name = "salary") | |
public Integer getSalary() { | |
return this.salary; | |
} | |
public void setSalary(Integer salary) { | |
this.salary = salary; | |
} | |
@Override | |
public String toString() { | |
StringBuffer sb = new StringBuffer(); | |
sb.append("Id: ").append(this.id).append(", firstName: ").append(this.firstname).append(", lastName: ") | |
.append(this.lastname).append(", Designation: ").append(this.designation).append(", Salary: ") | |
.append(this.salary); | |
return sb.toString(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bytestree.dao; | |
import com.bytestree.model.Employee; | |
public interface EmployeeDao extends GenericDao<Employee> { | |
Integer getMaxSalary(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bytestree.dao; | |
import org.hibernate.Criteria; | |
import org.hibernate.criterion.Projections; | |
import org.springframework.stereotype.Repository; | |
import com.bytestree.model.Employee; | |
@Repository | |
public class EmployeeDaoImpl extends AbstractGenericDao<Employee> implements EmployeeDao { | |
@Override | |
public Integer getMaxSalary() { | |
Criteria criteria = getSession().createCriteria(Employee.class).setProjection(Projections.max("salary")); | |
Integer maxSalary = (Integer) criteria.uniqueResult(); | |
return maxSalary; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bytestree.service; | |
import java.util.List; | |
import org.apache.log4j.Logger; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Service; | |
import org.springframework.transaction.annotation.Transactional; | |
import com.bytestree.dao.EmployeeDao; | |
import com.bytestree.model.Employee; | |
@Service | |
@Transactional(readOnly = true) | |
public class EmployeeServiceImpl implements EmployeeService { | |
final static Logger logger = Logger.getLogger(EmployeeServiceImpl.class); | |
@Autowired | |
EmployeeDao employeeDao; | |
public List<Employee> getAllEmployees() { | |
logger.debug("Getting all employees..."); | |
return employeeDao.findAll(); | |
} | |
/** | |
* Add new employee if it in not already exists | |
* | |
* @param employee: Employee to add | |
*/ | |
@Override | |
@Transactional(readOnly = false) | |
public void addNewEmployee(Employee employee) { | |
Employee emp = new Employee(); | |
emp.setFirstname(employee.getFirstname()); | |
emp.setLastname(employee.getLastname()); | |
List<Employee> emplList = employeeDao.findAllByExample(emp); | |
if (emplList == null || emplList.isEmpty()) { | |
Long id = (Long) employeeDao.save(employee); | |
logger.debug("Id of new Employee " + id); | |
} else { | |
logger.debug("Employee " + emp + " already exists"); | |
} | |
} | |
/** | |
* Return maximum salary given to any employee | |
* | |
* @return max salary | |
*/ | |
@Override | |
public Integer getMaxSalary() { | |
return employeeDao.getMaxSalary(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Refer Generic DAO in Hibernate for complete example