private Shred populateShred(Shred shred) { // int ownerId = shred.getOwner().getId(); // shred.setOwner(jdbcTemplate.queryForObject( // ShredderDAOImpl.GET_SHREDDER_BY_ID, // new Object [] {ownerId}, new ShredderMapper() // )); // Set rating String ratesql = "SELECT * FROM Rating WHERE ShredId=" + shred.getId(); ShredRating rating = jdbcTemplate.queryForObject( ratesql, new BeanPropertyRowMapper<ShredRating>(ShredRating.class)); shred.setRating(rating); shred.setShredComments(this.getShredCommentsForShredWithId(shred.getId())); // Set tags shred.setTags(this.getTagsForShredWithId(shred.getId())); return shred; }
public int persistShred(final Shred shred) { // Persist shred long t1 = System.currentTimeMillis(); final String sql = "INSERT INTO Shred VALUES (DEFAULT,?,?,?,?,?,?)"; KeyHolder keyHolder = new GeneratedKeyHolder(); jdbcTemplate.update( new PreparedStatementCreator() { public PreparedStatement createPreparedStatement(Connection connection) throws SQLException { PreparedStatement ps = connection.prepareStatement(sql, new String[] {"id"}); ps.setString(1, shred.getDescription()); ps.setLong(2, shred.getOwner().getId()); ps.setDate(3, shred.getTimeCreated()); ps.setString(4, shred.getVideoPath()); ps.setString(5, shred.getShredType()); ps.setString(6, shred.getThumbnailpath()); return ps; } }, keyHolder); shred.setId((Integer) keyHolder.getKey()); // Persist tags String insertTag = "INSERT INTO TagsForShred VALUES (?,?)"; ; if (shred.getTags() != null) { for (Tag t : shred.getTags()) { this.jdbcTemplate.update(insertTag, shred.getId(), t.getId()); } } // insert rating for this shred jdbcTemplate.update("INSERT INTO Rating VALUES(?, DEFAULT, DEFAULT)", shred.getId()); System.out.println("Persist shred: " + (System.currentTimeMillis() - t1)); logger.info("Persist shred: " + (System.currentTimeMillis() - t1)); return shred.getId(); }