Это потому, что каждый не равный удалит остальные записи.Одним из решений является использование jdbcTemplate.execute
путем динамического создания sql.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.JdbcTemplate;
@SpringBootApplication
public class Application implements CommandLineRunner {
private static final Logger log = LoggerFactory.getLogger(Application.class);
public static void main(final String args[]) {
SpringApplication.run(Application.class, args);
}
@Autowired
JdbcTemplate jdbcTemplate;
@Override
public void run(final String... strings) throws Exception {
log.info("Creating tables");
jdbcTemplate.execute("DROP TABLE votes IF EXISTS");
jdbcTemplate.execute("CREATE TABLE votes(id SERIAL, poll_id integer, option_id integer)");
final List<Object[]> splitUpValues = Arrays.asList("1 0", "1 1", "2 2", "1 3", "1 4").stream()
.map(name -> name.split(" ")).collect(Collectors.toList());
splitUpValues.forEach(name -> log.info(String.format("Inserting votes record for %d %d",
Integer.valueOf((String) name[0]), Integer.valueOf((String) name[1]))));
jdbcTemplate.batchUpdate("INSERT INTO votes(poll_id, option_id) VALUES (?,?)", splitUpValues);
log.info("Querying for all votes");
jdbcTemplate
.query("SELECT id, poll_id, option_id FROM votes",
(rs, rowNum) -> new Votes(rs.getLong("id"), rs.getInt("poll_id"), rs.getInt("option_id")))
.forEach(vote -> log.info(vote.toString()));
jdbcTemplate.execute("DELETE FROM votes WHERE poll_id = 1 AND option_id not in (1, 3)");
log.info("Querying for all votes after batch delete");
jdbcTemplate
.query("SELECT id, poll_id, option_id FROM votes",
(rs, rowNum) -> new Votes(rs.getLong("id"), rs.getInt("poll_id"), rs.getInt("option_id")))
.forEach(vote -> log.info(vote.toString()));
}
}