gpt4 book ai didi

java - 使用 @Query 使用 Spring Data 创建自定义查询

转载 作者:太空宇宙 更新时间:2023-11-04 11:53:18 25 4
gpt4 key购买 nike

我有一个消息实体和一个管理实体。消息是:

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

@Column(name = "text")
private String text;

@OneToOne
@JoinColumn(name = "id_from")
private Admin idFrom;

@OneToOne
@JoinColumn(name = "id_too")
private Admin idToo;

管理员是:

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

@Column(name = "username")
private String username;

@Column(name = "password")
private String password;

@Column(name = "email")
private String email;

如何创建此查询:

select id_from, text from message
where id_from = from and id_too = too
or id_from = too and id_too = from;

使用@Query注释?

类似这样的事情:

@Query("SELECT m FROM Message m WHERE m.id_from = :from.id and m.id_too = :too.id or m.id_from = :too.id and m.id_too = :from.id")
List<Message> findMessages(@Param("from") Admin from, @Param("too") Admin too);

谢谢

最佳答案

@Query("SELECT m FROM Message m WHERE m.idFrom.id = :fromId and m.idToo.id = :tooId or m.idFrom.id = :tooId and m.idToo.id = :fromId")
List<Message> findMessages(@Param("fromId") Long fromId, @Param("tooId") Long tooId);

关于java - 使用 @Query 使用 Spring Data 创建自定义查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41593910/

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