gpt4 book ai didi

java - 同时向两个表插入数据。 h2数据库

转载 作者:行者123 更新时间:2023-12-01 21:13:12 26 4
gpt4 key购买 nike

我在编写 SQL 查询时遇到问题。我想将数据插入到我的 h2 数据库中。我的应用程序使用 spring boot 和内存 h2 数据库。

我的两个实体:

@Entity
public class Movie {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
@OneToMany(cascade = CascadeType.ALL)
private List<ScreeningTime> screeningTime;
private int screeningRoomId;
}

@Entity
public class ScreeningTime {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private long screeningTime;
}

我想使用 SQL 查询插入数据:

INSERT INTO PUBLIC.MOVIES (TITLE, SCREENING_TIME, SCREENING_ROOM_ID) 
VALUES ('Film1', 1573399800000, 1);

我的查询错误,但我不知道如何修复它。你能帮我吗?

最佳答案

简单地使用spring data jpa

在您的项目中创建存储库。帮助https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#reference

ScreeningTime time1=new ScreeningTime ();
time1.setScreeningTime("yourtime");

ScreeningTime time2=new ScreeningTime ();
time2.setScreeningTime("yourtime");

List<ScreeningTime> times=new ArrayList<>();
times.add(time1);
times.add(time2);

Movie m=new Movie();
m.setTitle("Film1");
m.setScreeningRoomId(1);
m.setScreeningTime(times);

repository.save(m);

关于java - 同时向两个表插入数据。 h2数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58892752/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com