简体   繁体   中英

hibernate keeps updating the values of the first record instead of creating new record

I have an autoincrement set to my primary key but when i perform save in while loop it inserts first data with primary key 1 and second data doesnt get inserted as primary key2 but rather updates record one where primary is 1

so i am getting

2017-01-01 00:00:11.763|192.168.234.82|"GET / HTTP/1.1"|200|"swcd (unknown version) CFNetwork/808.2.16 Darwin/15.6.0"
Hibernate: insert into log_bean (date_time, ip_address, request, status, user_agent) values (?, ?, ?, ?, ?)
2017-01-01 00:00:21.164|192.168.234.82|"GET / HTTP/1.1"|200|"swcd (unknown version) CFNetwork/808.2.16 Darwin/15.6.0"
Hibernate: select logbean0_.id as id1_0_0_, logbean0_.date_time as date_tim2_0_0_, logbean0_.ip_address as ip_addre3_0_0_, logbean0_.request as request4_0_0_, logbean0_.status as status5_0_0_, logbean0_.user_agent as user_age6_0_0_ from log_bean logbean0_ where logbean0_.id=?
Hibernate: update log_bean set date_time=?, ip_address=?, request=?, status=?, user_agent=? where id=?
2017-01-01 00:00:23.003|192.168.169.194|"GET / HTTP/1.1"|200|"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393"
Hibernate: select logbean0_.id as id1_0_0_, logbean0_.date_time as date_tim2_0_0_, logbean0_.ip_address as ip_addre3_0_0_, logbean0_.request as request4_0_0_, logbean0_.status as status5_0_0_, logbean0_.user_agent as user_age6_0_0_ from log_bean logbean0_ where logbean0_.id=?
Hibernate: update log_bean set date_time=?, ip_address=?, request=?, status=?, user_agent=? where id=?
2017-01-01 00:00:40.554|192.168.234.82|"GET / HTTP/1.1"|200|"swcd (unknown version) CFNetwork/808.2.16 Darwin/15.6.0"

updates instead of inserts here is my code

public void readFile(LogBean logBean) throws IOException {

        Scanner read = new Scanner(
            new File(
                "/home/user/MyProjects/java-recruitment-task/parserproject/src/main/resources/access.txt"));

        while (read.hasNext()) { //checks if there is a valid token

            String string = read.nextLine();
            System.out.println(string);

            Scanner readFileByLine = new Scanner(string);

            while (readFileByLine.hasNext()) { //checks valid token if not then goes out of loop
                String[] split = readFileByLine.nextLine().split("\\|");

                logBean.setDateTime(LocalDateTime.parse(split[0], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")));
                logBean.setIp_address(split[1]);
                logBean.setRequest(split[2]);
                logBean.setStatus(split[3]);
                logBean.setUserAgent(split[4]);
            }
            logbeanRepository.save(logBean);
        }

    }

and my bean is

@Entity
public class LogBean {

    @Id
    @Column(name = "id", updatable = false, nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

what am i doing wrong ? this is in spring boot so my db properties are app.properties

You are using the same "logBean" item every time.

Since you try to save the same object, even if you have modified the attributes, it IS the same item, and that's why when you save it, hybernate updates the item instead of creating a new one.

To solve it, change your method to:

public void readFile() throws IOException {

    Scanner read = new Scanner(
        new File(
            "/home/user/MyProjects/java-recruitment-task/parserproject/src/main/resources/access.txt"));

    while (read.hasNext()) { //checks if there is a valid token

        String string = read.nextLine();
        System.out.println(string);

        Scanner readFileByLine = new Scanner(string);

        while (readFileByLine.hasNext()) { //checks valid token if not then goes out of loop
            String[] split = readFileByLine.nextLine().split("\\|");
            LogBean logBean = new LogBean();

            logBean.setDateTime(LocalDateTime.parse(split[0], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")));
            logBean.setIp_address(split[1]);
            logBean.setRequest(split[2]);
            logBean.setStatus(split[3]);
            logBean.setUserAgent(split[4]);
        }
        logbeanRepository.save(logBean);
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM