Java Spring hibernate HQL where子句不起作用

Java Spring hibernate HQL where子句不起作用,java,spring,hibernate,join,where-clause,Java,Spring,Hibernate,Join,Where Clause,我有一个带有联接的HQL查询,但联接实体上的where子句(instrPrice.date BETWEEN:dateFrom和:dateTo)不起作用。查询始终返回instrumentPrice的所有记录,而不是按日期限制结果 命名查询 @NamedQuery(name=“findAllPrices”, query=“从TaPatternInstrument taPat中选择不同的taPat” +“左连接提取taPat.instrument instr” +“左连接仪表价格表仪表” +“其中ta

我有一个带有联接的HQL查询,但联接实体上的where子句(instrPrice.date BETWEEN:dateFrom和:dateTo)不起作用。查询始终返回instrumentPrice的所有记录,而不是按日期限制结果

命名查询

@NamedQuery(name=“findAllPrices”,
query=“从TaPatternInstrument taPat中选择不同的taPat”
+“左连接提取taPat.instrument instr”
+“左连接仪表价格表仪表”
+“其中taPat.id=:taPatternInstrumentId”
+“和instrPrice.date介于:dateFrom和:dateTo之间”)
调用查询的服务

public TaPatternInstrument FindAllPrice(int-taPatternInstrumentId、LocalDate-dateFrom、LocalDate-dateTo){
TypedQuery TypedQuery=createNamedQuery(“findAllPrices”,
TaPatternInstrument.class);
setParameter(“taPatternInstrumentId”,taPatternInstrumentId);
setParameter(“dateFrom”,dateFrom);
setParameter(“dateTo”,dateTo);
返回typedQuery.getSingleResult();
}
实体

公共抽象类BaseEntity实现可序列化{
@身份证
@列(name=“id”)
@生成价值(策略)=
GenerationType.IDENTITY)
受保护的整数id。。。
}
公共类TaPatternInstrument扩展了BaseEntity{
@manytone(fetch=FetchType.EAGER)
@JoinColumn(name=“instrument\u id”,nullable=false,foreignKey=@foreignKey(name=
“tapatterninstrument\u instrument\u fk”))
私人文书;
}
公共类工具扩展了基本实体{
@OneToMany(mappedBy=“instrument”,fetch=FetchType.LAZY,cascade=CascadeType.ALL)
私人清单和价格清单;
}
生成的SQL
解决方案是在instrumentPriceList上添加一个FETCH:左连接FETCH instr.instrumentPriceList instrPrice

@NamedQuery(name = "findAllPrices", 
query = "SELECT DISTINCT taPat FROM TaPatternInstrument taPat "
        + "LEFT JOIN FETCH taPat.instrument instr "
        + "LEFT JOIN FETCH instr.instrumentPriceList instrPrice "
        + "LEFT JOIN taPat.taPatternInstrumentPriceList taPatpr "
        + "WHERE taPat.id = :taPatternInstrumentId "
        + "AND instrPrice.date BETWEEN :dateFrom AND :dateTo ")
FETCH强制Hibernate在第一个DB请求时立即检索实体(InstrumentPrice)。因此,where子句被考虑在内。
如果没有FETCH,则只有在调用实体工具的getInstrumentPriceList方法(执行对DB的附加调用)时,才会从DB中检索实体InstrumentPrice。通过对DB的额外调用,不再考虑where子句,从而从Entity instrumentPrice检索所有记录。

您是否尝试使用任何其他类型的日期?此问题是否仅与LocalDate相关?哪个是您的数据集?或者是一个例子?另外,您是否在带有本机sql的DMBS中使用此查询来检查查询是否正常?我必须使用LocalDate,因为在entity InstrumentPrice中,属性日期的定义如下:private LocalDate;我已经在发布的问题末尾添加了生成的sql。它给出的错误是什么?Stacktrace?
@NamedQuery(name = "findAllPrices", 
query = "SELECT DISTINCT taPat FROM TaPatternInstrument taPat "
        + "LEFT JOIN FETCH taPat.instrument instr "
        + "LEFT JOIN FETCH instr.instrumentPriceList instrPrice "
        + "LEFT JOIN taPat.taPatternInstrumentPriceList taPatpr "
        + "WHERE taPat.id = :taPatternInstrumentId "
        + "AND instrPrice.date BETWEEN :dateFrom AND :dateTo ")