spring数据存储库查询包含集合

时间:2015-07-07 14:00:44

标签: java spring hibernate jpa spring-data

我在使用spring data jpa存储库执行自定义查询时遇到问题。

我有一个实现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;

1 个答案:

答案 0 :(得分:2)

这是您的查询:

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在哪里加入?,如果将此查询转换为本机查询,则可以检测到错误。