Quickstart

1. Add Hades to your classpath

Hades is hosted in the central Maven repository so integrating Hades is just a matter of declaring the dependency:

1 <dependency>
2     <groupId>org.synyx.hades</groupId>
3     <artifactId>org.synyx.hades</artifactId>
4     <version>1.6.1.RELEASE</version>
5 </dependency>

2. Create a JPA entity

 1 @Entity
 2 @NamedQuery(id="User.findByLastname" query="from User u where u.lastname = ?1")
 3 public class User {
 4 
 5   @Id
 6   private Long id;
 7   private String username;
 8   private String lastname;
 9   private int age;
10 }

3. Create a DAO interface with query methods

 1 public interface UserDao extends GenericDao<User, Long> {
 2 
 3   // Will trigger the NamedQuery due to a naming convention
 4   List<User> findByLastname(String lastname);
 5 
 6   // Will create a query from the methodname
 7   // from User u where u.username = ?
 8   User findByUsername(String username);
 9 
10   // Uses query annotated to the finder method in case you
11   // don't want to pollute entity with query info
12   @Query("from User u where u.age > ?1")
13   List<User> findByAgeGreaterThan(int age);
14 }

4a. Kickstart Hades in Spring

1 <hades:dao-config base-package="com.acme.*.dao" />

4b. Use the DAO programatically

 1 EntityManager em = Persistence.getEntityManagerFactory().createEntityManager();
 2 GenericDaoFactory factory = GenericDaoFactory.create(em);
 3 
 4 UserDao dao = factory.getDao(UserDao.class);
 5 
 6 // Trigger the finders
 7 List<User> adults = dao.findByAgeGreaterThan(18);
 8 
 9 // Get pagination out of the box
10 Page<User> users = dao.readAll(new PageRequest(0, 20));

5. More features

  • Integrate with Spring to avoid manual setup of DAO instances
  • Integrate your custom DAO code
  • Apply auditing to entities

6. Next steps?

Also available in: HTML TXT