java - 同时向两个表中插入数据。h2 数据库
问题描述
我在编写 SQL 查询时遇到问题。我想将数据插入我的 h2 数据库。我的应用程序使用带有内存 h2 数据库的 spring boot。
我的两个实体:
@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);
我的查询是错误的,但我不知道如何解决它。你可以帮帮我吗?
解决方案
添加屏幕时间。你可以看电影
@Repository
public interface ScreeningTime extends CrudRepository<ScreeningTime , Long> {
@Modifying
@Query(value = "insert into ScreeningTime (id,screeningTime) VALUES (:id,:screeningTime)", nativeQuery = true)
@Transactional
void saveScreen(@Param("id") String id, @Param("screeningTime") Long screeningTime);
}
推荐阅读
- python - Flask-Mail 电子邮件未发送
- android - 使用assetlinks.json 排除或仅允许某些网址
- javascript - 是否可以使用 grecaptcha.execute 绕过 hCaptcha?
- javascript - 如何将节点模块“child_process”导入 Angular 项目?
- java - 带有gradle的java 9模块不起作用:错误:找不到模块:
- php - 如何从 GitHub 在 laravel 中添加 SDK?
- python-ldap - 安装python ldap包时出错
- javascript - 如何在另一个函数完成读取输入的每个字母后终止 setInterval 函数?
- javascript - 如何使用 DOM 元素的文本内容来引用命名导入?
- react-native - react native 如何在模态中并排使用两个项目?