我有 4 个实体 MultiPlex、Screen、Screening 和 Movie。我想写2个查询。根据给定 MultiPlexId 的屏幕名称查找屏幕是否存在。(假设 id 1 的 Multiplex 有 4 个屏幕,Audi-1、Audi-2、Audi-3 和 Audi-4,查询应该能够找到 id 1 的 Multiplex 中是否存在 Audi-2)查找在给定日期运行给定电影的所有多厅影院(放映有屏幕、电影和日期的参考)我的实体电影:-@Entitypublic class Movie { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; private String title; private double rating;}多重:-@Entitypublic class Multiplex { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; private String name; @OneToMany(mappedBy = "multiplex", cascade = CascadeType.ALL) private List<Screen> screens;}屏幕:-@Entitypublic class Screen { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; private String name; @ManyToOne private Multiplex multiplex;}筛选:-@Entitypublic class Screening { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; @OneToOne private Screen screen; @OneToOne private Movie movie; private LocalDate date; private LocalTime time; private float pricePerSeat;}对于我尝试的第一个查询@Query("SELECT m FROM Multiplex m Where m.id = :mId and m.screens.name = :screenName") public Optional<Multiplex> findScreenByName(@Param("mId") long id, @Param("screenName") String name);
1 回答
慕娘9325324
TA贡献1783条经验 获得超4个赞
m.screens
不是一个Screen
字段,而是List<Screen>
. 所以表达m.screens.name:=screenName
是错误的。
尝试这个
1)
@Query("select m from Multiplex m, Screen s where m.id=s.multiplex.id and m.id =:mId and s.name =:screenName") public Optional<Multiplex> findByMultiplexIdAndScreenName(@Param("mId") long id, @Param("screenName") String name);
2)
@Query("select distinct m from Multiplex m, Screening s where m.id=s.screen.multiplex.id and s.movie=:movie and s.date =:date") public List<Multiplex> findAllByMovieAndDate(@Param("movie") Movie movie, @Param("date") LocalDate date);
或者
@Query("select distinct s.screen.multiplex from Screening s where s.movie=:movie and s.date =:date") public List<Multiplex> findAllByMovieAndDate(@Param("movie") Movie movie, @Param("date") LocalDate date);
通过使用最后一个,如果决定将 FetchTypes 更改为 Lazy,您可能会遇到异常
添加回答
举报
0/150
提交
取消