Java hibernate不断更新第一条记录的值,而不是创建新记录
我的主键设置了一个自动增量,但当我执行“保存在while循环”时,它会插入主键为1的第一个数据,第二个数据不会作为主键2插入,而是更新主键为1的记录1 所以我得到了Java hibernate不断更新第一条记录的值,而不是创建新记录,java,spring,jpa,Java,Spring,Jpa,我的主键设置了一个自动增量,但当我执行“保存在while循环”时,它会插入主键为1的第一个数据,第二个数据不会作为主键2插入,而是更新主键为1的记录1 所以我得到了 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_ad
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"
更新而不是插入
这是我的密码
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);
}
}
我的豆子是
@Entity
public class LogBean {
@Id
@Column(name = "id", updatable = false, nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
我做错了什么?这是在spring boot中,因此my db属性是app.properties您每次都使用相同的“logBean”项
由于您试图保存相同的对象,即使您修改了属性,它也是相同的项,这就是为什么在保存它时,hybernate会更新该项,而不是创建一个新项
要解决此问题,请将方法更改为:
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);
}
}
在
readFile
内部创建一个新的LogBean实例,而不是将其传递给该方法?正如Joakim指出的,当您只是不断地重新设置(更新)一个对象(行)的值时,为什么您希望生成新的记录?您的意思是在while循环中我应该创建一个新的LogBean实例@是的,请参见下面的解决方案