spring 数据存储库查询包含集合
spring data repository query included collection
我在使用 spring 数据 jpa 存储库执行自定义查询时遇到问题。
我有一个存储库 class 实现了 JPARepository<>。对于所有内置 CRUD 查询以及一些自定义查询,一切都按预期工作,但在内部集合之间进行限定不起作用,并且返回完整的结果集,就好像集合的限定不存在一样。
例如,这里有一个查询:
public interface MessageRepository extends JpaRepository<Message, Integer> {
@Query("SELECT a FROM Message a, Message_Topic b WHERE a.systemNm = :theSystem AND a. applicationNm = :theApplication AND b.topicNm = :theTopicName AND a.insertTs BETWEEN :theStartDate AND :theEndDate AND a.expirationDt > CURRENT_TIMESTAMP")
List<Message> findMessagesByTopic(@Param("theSystem") String theSystem,
@Param("theApplication") String theApplication,
@Param("theTopicName") String theTopicName,
@Param("theStartDate") Date theStartDate,
@Param("theEndDate") Date theEndDate);
具有以下 JPA 实体:
留言:
@Entity
public class Message implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="message_id")
private int messageId;
@Column(name="application_nm")
private String applicationNm;
@Column(name="execution_instance_txt")
private String executionInstanceTxt;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="expiration_dt")
private Date expirationDt;
@Column(name="grouping_des")
private String groupingDes;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="insert_ts")
private Date insertTs;
@Column(name="message_detail_txt")
private String messageDetailTxt;
@Column(name="message_summary_txt")
private String messageSummaryTxt;
@Column(name="severity_des")
private String severityDes;
@Column(name="system_nm")
private String systemNm;
//uni-directional many-to-one association to Message_Topic
@OneToMany(fetch=FetchType.EAGER)
@JoinColumn(name="message_id", referencedColumnName="message_id")
private Set<Message_Topic> messageTopics;
Message_Topic:
@Entity
public class Message_Topic implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="message_topic_id")
private int messageTopicId;
@Column(name="message_id", insertable=false, updatable=false)
private int messageId;
@Column(name="topic_nm")
private String topicNm;
@Column(name="topic_value_txt")
private String topicValueTxt;
这是您的查询:
SELECT a FROM Message a, Message_Topic b WHERE a.systemNm = :theSystem AND a. applicationNm = :theApplication AND b.topicNm = :theTopicName AND a.insertTs BETWEEN :theStartDate AND :theEndDate AND a.expirationDt > CURRENT_TIMESTAMP
Message 和 Message_Topic 连接在哪里?,如果将此查询转换为本机查询,则可能会检测到错误。
我在使用 spring 数据 jpa 存储库执行自定义查询时遇到问题。
我有一个存储库 class 实现了 JPARepository<>。对于所有内置 CRUD 查询以及一些自定义查询,一切都按预期工作,但在内部集合之间进行限定不起作用,并且返回完整的结果集,就好像集合的限定不存在一样。
例如,这里有一个查询:
public interface MessageRepository extends JpaRepository<Message, Integer> {
@Query("SELECT a FROM Message a, Message_Topic b WHERE a.systemNm = :theSystem AND a. applicationNm = :theApplication AND b.topicNm = :theTopicName AND a.insertTs BETWEEN :theStartDate AND :theEndDate AND a.expirationDt > CURRENT_TIMESTAMP")
List<Message> findMessagesByTopic(@Param("theSystem") String theSystem,
@Param("theApplication") String theApplication,
@Param("theTopicName") String theTopicName,
@Param("theStartDate") Date theStartDate,
@Param("theEndDate") Date theEndDate);
具有以下 JPA 实体:
留言:
@Entity
public class Message implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="message_id")
private int messageId;
@Column(name="application_nm")
private String applicationNm;
@Column(name="execution_instance_txt")
private String executionInstanceTxt;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="expiration_dt")
private Date expirationDt;
@Column(name="grouping_des")
private String groupingDes;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="insert_ts")
private Date insertTs;
@Column(name="message_detail_txt")
private String messageDetailTxt;
@Column(name="message_summary_txt")
private String messageSummaryTxt;
@Column(name="severity_des")
private String severityDes;
@Column(name="system_nm")
private String systemNm;
//uni-directional many-to-one association to Message_Topic
@OneToMany(fetch=FetchType.EAGER)
@JoinColumn(name="message_id", referencedColumnName="message_id")
private Set<Message_Topic> messageTopics;
Message_Topic:
@Entity
public class Message_Topic implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="message_topic_id")
private int messageTopicId;
@Column(name="message_id", insertable=false, updatable=false)
private int messageId;
@Column(name="topic_nm")
private String topicNm;
@Column(name="topic_value_txt")
private String topicValueTxt;
这是您的查询:
SELECT a FROM Message a, Message_Topic b WHERE a.systemNm = :theSystem AND a. applicationNm = :theApplication AND b.topicNm = :theTopicName AND a.insertTs BETWEEN :theStartDate AND :theEndDate AND a.expirationDt > CURRENT_TIMESTAMP
Message 和 Message_Topic 连接在哪里?,如果将此查询转换为本机查询,则可能会检测到错误。