title: Java DAO classes for many-to-one relationship
body: <p>I have a problem regarding how to save the objects in the Database in Java when having many-to-one relationship.</p>
  
  <p>Basically I have 2 classes - UserVO and GroupVO, that look like this:</p>
  
  <pre><code>public class UserVO extends ValueObject implements Serializable {
  
      /**
       * The default serial version ID
       */ 
      private static final long serialVersionUID = 1L;
  
      private String  login;
      private String  password;
      private Long    groupId;
  
      public UserVO() {
          super();
  
          this.setLogin("");
          this.setPassword("");
          this.setGroupId(0L);
      }
  
          // all the getters and setters
          // ...
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>public final class GroupVO extends ValueObject implements Serializable {
  
      /**
       * The default serial version ID
       */ 
      private static final long serialVersionUID = 1L;
  
      private String      description;    
      private Set&lt;UserVO&gt; users = new HashSet&lt;UserVO&gt;();
  
      public GroupVO() {
          super();
  
          this.setDescription("");
      }
  
          // all the getters and setters
          // ...
  }
  </code></pre>
  
  <p>Their super-class is a very simple abstract class:</p>
  
  <pre><code>public abstract class ValueObject {
  
      private Long    id;
      private String  name;
  
      public ValueObject() {
          super();
  
          // the ID is auto-generated
  //      this.setId(0L);
          this.setName("");
      }
  
          // all the getters and setters
          // ...
  }
  </code></pre>
  
  <p>Now I have to create the DAO classes for them. In the UserDAO I something like this for creating and inserting a user in the DB:</p>
  
  <pre><code>@Override
      public Long create(UserVO user) throws IllegalArgumentException, DAOException {
          if (user.getId() != null) {
              throw new IllegalArgumentException("User may already be created, the user ID is not null.");
          }
  
          Object[] values = { user.getName(), user.getLogin(), user.getPassword(), user.getGroupId() };
  
          Connection          connection          = null;
          PreparedStatement   preparedStatement   = null;
          ResultSet           generatedKeys       = null;
  
          try {
              connection = daoFactory.getConnection();
              preparedStatement = DAOUtil.prepareStatement(connection, SQL_CREATE_USER, true, values);
  
              int affectedRows = preparedStatement.executeUpdate();
  
              if (affectedRows == 0) {
                  throw new DAOException("Creating user failed, no rows affected.");
              }
  
              generatedKeys = preparedStatement.getGeneratedKeys();
  
              if (generatedKeys.next()) {
                  user.setId(generatedKeys.getLong(1));
              } else {
                  throw new DAOException("Creating user failed, no generated key obtained.");
              }
          } catch (SQLException e) {
              throw new DAOException(e);
          } finally {
              DAOUtil.close(connection, preparedStatement, generatedKeys);
          }
  
          return user.getId();
      }
  </code></pre>
  
  <p>There are also some helper Classes, but I guess you understand my code :).</p>
  
  <p>And this is the GroupDAO create method:</p>
  
  <pre><code>@Override
      public Long create(GroupVO group) throws IllegalArgumentException, DAOException {
          if (group.getId() != null) {
              throw new IllegalArgumentException("Group may already be created, the group ID is not null.");
          }
  
          Object[] values = { group.getName(), group.getDescription() };
  
          Connection          connection          = null;
          PreparedStatement   preparedStatement   = null;
          ResultSet           generatedKeys       = null;
  
          try {
              connection = daoFactory.getConnection();
              preparedStatement = DAOUtil.prepareStatement(connection, SQL_CREATE_GROUP, true, values);
  
              int affectedRows = preparedStatement.executeUpdate();
  
              if (affectedRows == 0) {
                  throw new DAOException("Creating group failed, no rows affected.");
              }
  
              generatedKeys = preparedStatement.getGeneratedKeys();
  
              if (generatedKeys.next()) {
                  group.setId(generatedKeys.getLong(1));
              } else {
                  throw new DAOException("Creating group failed, no generated key obtained.");
              }
  
          } catch (SQLException e) {
              throw new DAOException(e);
          } finally {
              DAOUtil.close(connection, preparedStatement, generatedKeys);
          }
  
          return group.getId();
      }
  </code></pre>
  
  <p>Now, if I make a small test in the main function, for creating a group and saving in the DB, everything goes well:</p>
  
  <pre><code>DAOFactory usersGroupsRolesFactory = DAOFactory.getInstance("UsersGroupsRolesDB.jdbc");
  System.out.println("DAOFactory successfully obtained: " + usersGroupsRolesFactory);
  
  // Create an instance of the GroupDAO class
  GroupDAO dao = usersGroupsRolesFactory.getGroupDAO();
  
  // Create some GroupVO objects
  GroupVO group1 = new GroupVO();
  group1.setName("Administrators");
  group1.setDescription("These users have all the right in the application");
  
  dao.create(group1);
  </code></pre>
  
  <p>As GroupVO class has a set of UserVO objects, and in the main function if I also type:</p>
  
  <pre><code>UserVO user1 = new UserVO();
  user1.setName("Palancica Pavel");
  user1.setLogin("login_pavel");
  user1.setPassword("password_pavel");
  
  group1.getUsers().add(user1); // I may also add some more users
  </code></pre>
  
  <p>and say I am first time calling: dao.create(group1);</p>
  
  <p>Normally, this shouldn't only save the Group info, but also all the associated UserVO objects.</p>
  
  <p>For me that means that in the "create" function of the GroupDAO, after the group ID is successfully generated, I need to do a lot of other code.</p>
  
  <p>I wonder is this is the correct way of saving those users in the DB, as I think I have to make the GroupDAO class to communicate with the UserDAO class, and also with the DAOFactory, which in my case, can give us an UserDAO, or GroupDAO object. Or I can do all the DB interection for saving those users without using the UserDAO class.</p>
  
  <p>That code that I'm thinking seem very long, messy/spaghetti, and I am not quite sure if this is the correct approach :(.</p>
  
  <p>Note that I am note using any ORM Framework.</p>
  
  <p>Please let me know guys, what do you think about that?! 
  If you need more details, I can send you my Project, it's not commercial :D</p>
  
  <p>Thanks in advance</p>