简体   繁体   English

休眠保持更新第一条记录的值,而不是创建新记录

[英]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 我将自动增量设置为我的主键,但是当我在while循环中执行保存时,它会插入具有主键1的第一个数据,而不会将第二个数据作为主键2插入,而是更新记录其中主键为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 这是在春季启动,所以我的数据库属性是app.properties

You are using the same "logBean" item every time. 您每次都使用相同的“ logBean”项。

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. 由于您尝试保存同一对象,因此即使您修改了属性,该对象也属于同一项目,这就是为什么保存时,hybernate会更新该项目而不是创建一个新对象。

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);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Hibernate没有更新记录 - Wicket - Hibernate is not updating record - Wicket Hibernate 不更新数据库记录 - Hibernate is not updating a database record 休眠:新记录或旧记录 - Hibernate: new or old record 如何告诉 Hibernate 检索 id 而不是插入新记录 - How to tell Hibernate to retrieve the id instead of inserting a new record Hibernate saveOrUpdate不更新而是创建新记录 - Hibernate saveOrUpdate does not update instead creates new record 休眠检查更新记录是否成功 - Hibernate check if updating a record succeeded Hibernate saveOrUpdate不更新现有记录,而是将记录另存为新条目 - Hibernate saveOrUpdate does not update existing record instead it saves the record as new entry 为什么Hibernate SchemaUpdate是创建新字段而不是更新? - Why Hibernate SchemaUpdate is creating new field instead of updating? Spring Boot 中的处理程序方法正在保存新记录,而不是更新数据库中已存在的记录以进行 put 请求 - handler method in spring boot is saving new record instead of updating already existed record in database for put request 从 hibernate 插入记录时,触发器未更新当前记录 PostgresSQL - Trigger is not updating current record PostgresSQL when record is inserted from hibernate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM