2.3. Basic entities and DAOs

Now we are going to create the entities and DAOs needed to manage users with roles. Note that we will start with an implementation with the least possible coupling to Hades. The will force us to configure a few things ourselves but decrease dependencies. If you want to know, how you can ease things a little more see ??? and Section 2.4, “Easing configuration - Spring namespaces”.

2.3.1. Users and roles

As we want to create a small user management application, the primary entities we will deal with are User and Role. Take a look at the code snippets:

Example 2.6. User entity

@Entity
public class User {

  public static final long serialVersionUID = 123L

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;
  private String username;
  private String password;
  private String emailAddress;

  @ManyToMany
  private Set<Role> roles;

  public Long getId() {
    return this.id;
  }

  // Further getters and setters omitted

  public boolean isNew() {
    return null == this.id;
  }
}

Note that we decide to have an id type of Long. We have to add annotations for the id ourselves and implement isNew() manually, too. We create a many-to-many relationship to the Role class and store some string values like username, password and email address.

Example 2.7. Role entity

@Entity
public class Role {

  private static final long serialVersionUID = -123L;

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Integer id;
  private String name;

  public Integer getId() {
    return this.id;
  }

  // Further getters and setters omitted

  public boolean isNew() {
    return null == this.id;
  }
}

The Role class looks quite similar. Note that we decide for an alternate id type. So mixing id type is no problem at all.

2.3.2. DAOs

As we have modeled the entities we need to add an interface for a DAO handling persistence operations for them:

Example 2.8. UserDao and RoleDao interface

public interface UserDao extends GenericDao<User, Long> {
}
public interface RoleDao extends GenericDao<Role, Integer> {
}

As you can see the interfaces serve typing purposes only for now. We will add further functionality to them later. GenericDao provides most of the required persistence operations. Hades provides various implementations of this interface so that you don't need to code one on your own.


2.3.3. Configuration

GenericJpaDao is the base implementation of GenericDao that serves basic purposes very well. It uses Spring's exception translation and entity manager injection facilities so that its creation logic is encapsulated into GenericDaoFactoryBean. So the basic configuration for a DAO instance looks as follows:

Example 2.9. Simple DAO configuration

<import resource="infrastructure.xml" />                                                 (1)

<bean id="userDao" 
    class="org.synyx.hades.dao.orm.GenericDaoFactoryBean">
  <property name="daoInterface" value="org.synyx.hades.dao.UserDao" />
</bean>                                                                                  (2)

<bean id="roleDao"
    class="org.synyx.hades.dao.orm.GenericDaoFactoryBean">
  <property name="daoInterface" value="org.synyx.hades.dao.RoleDao" />
</bean>

(1)

Import infrastructure configuration. Supposed we have combined the configuration examples shown in Section 2.2, “Infrastructure” into a file named infrastructure.xml

(2)

Declare the DAO interface you want to expose. This interface has to extend GenericDao.


This is smallest standard XML configuration possible. There are a lot more options on GenericDaoFactoryBean, but you rather choose XML namespace configuration due to less verbosity.

2.3.4. Client code

The previously declared UserDao can be accessed via its bean id from the ApplicationContext, although you definately are better off letting the container inject it into you client:

Example 2.10. Manual lookup of UserDao

ApplicationContext ctx = new ClasspathXmlApplicationContext(
  "applicationContext.xml");
UserDao userDao = (UserDao) ctx.getBean("userDao");

User user = userDao.readByPrimaryKey(1L);