Skip to content

Instantly share code, notes, and snippets.

@javaeeeee
Created November 6, 2015 13:04
Show Gist options
  • Save javaeeeee/dce9423404af0a2e85f7 to your computer and use it in GitHub Desktop.
Save javaeeeee/dce9423404af0a2e85f7 to your computer and use it in GitHub Desktop.
A Hibernate DAO class for a Dropwizard project
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