Created
November 6, 2015 13:04
-
-
Save javaeeeee/dce9423404af0a2e85f7 to your computer and use it in GitHub Desktop.
A Hibernate DAO class for a Dropwizard project
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
public class EmployeeDAO extends AbstractDAO<Employee> { | |
/** | |
* Constructor. | |
* | |
* @param sessionFactory Hibernate session factory. | |
*/ | |
public EmployeeDAO(SessionFactory sessionFactory) { | |
super(sessionFactory); | |
} | |
/** | |
* Method returns all employees stored in the database. | |
* | |
* @return list of all employees stored in the database | |
*/ | |
public List<Employee> findAll() { | |
return list(namedQuery("com.javaeeeee.dwstart.core.Employee.findAll")); | |
} | |
/** | |
* Looks for employees whose first or last name contains the passed | |
* parameter as a substring. | |
* | |
* @param name query parameter | |
* @return list of employees whose first or last name contains the passed | |
* parameter as a substring. | |
*/ | |
public List<Employee> findByName(String name) { | |
StringBuilder builder = new StringBuilder("%"); | |
builder.append(name).append("%"); | |
return list( | |
namedQuery("com.javaeeeee.dwstart.core.Employee.findByName") | |
.setParameter("name", builder.toString()) | |
); | |
} | |
/** | |
* Method looks for an employee by her id. | |
* | |
* @param id the id of an employee we are looking for. | |
* @return Optional containing the found employee or an empty Optional | |
* otherwise. | |
*/ | |
public Optional<Employee> findById(long id) { | |
return Optional.fromNullable(get(id)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment