首页 > 解决方案 > 同时向两个表中插入数据。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);

我的查询是错误的,但我不知道如何解决它。你可以帮帮我吗?

标签: javasqlspring-boot

解决方案


添加屏幕时间。你可以看电影

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

推荐阅读