Пример #1
0
 private void validate(Vote vote) {
   if (!vote.getAnswer().isSaved()) {
     throw new IllegalStateException("Answer of " + vote + " should be saved first");
   }
   if (vote.getAuthor() != null && !vote.getAuthor().isSaved()) {
     throw new IllegalStateException("Author of " + vote + " should be saved first");
   }
 }
Пример #2
0
 @Override
 protected PreparedStatement getInsertStatement(Vote vote) throws SQLException {
   validate(vote);
   PreparedStatement statement =
       getConnection().prepareStatement(INSERT_QUERY, Statement.RETURN_GENERATED_KEYS);
   try {
     User author = vote.getAuthor();
     if (author == null) {
       statement.setNull(1, Types.INTEGER);
     } else {
       statement.setLong(1, author.getId());
     }
     statement.setLong(2, vote.getAnswer().getId());
     statement.setTimestamp(3, DatabaseUtil.dateToTimestamp(vote.getCreationDate()));
     return statement;
   } catch (SQLException e) {
     statement.close();
     throw e;
   }
 }