【问题标题】:Java Spring hibernate HQL where clause not workingJava Spring休眠HQL where子句不起作用
【发布时间】:2020-06-20 01:43:09
【问题描述】:

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

命名查询

@NamedQuery(name = "findAllPrices", 
        query = "SELECT DISTINCT taPat FROM TaPatternInstrument taPat "
                + "LEFT JOIN FETCH taPat.instrument instr "
                + "LEFT JOIN instr.instrumentPriceList instrPrice "
                + "WHERE taPat.id = :taPatternInstrumentId "
                + "AND instrPrice.date BETWEEN :dateFrom AND :dateTo ")

调用查询的服务

public TaPatternInstrument findAllPrices(int taPatternInstrumentId, LocalDate dateFrom,  LocalDate dateTo) {

   TypedQuery<TaPatternInstrument> typedQuery = createNamedQuery("findAllPrices", 
        TaPatternInstrument.class);
   typedQuery.setParameter("taPatternInstrumentId", taPatternInstrumentId);
   typedQuery.setParameter("dateFrom", dateFrom);
   typedQuery.setParameter("dateTo", dateTo);
   return typedQuery.getSingleResult(); 
}

实体

public abstract class BaseEntity implements Serializable {

   @Id  
   @Column(name = "id")     
   @GeneratedValue(strategy =
                GenerationType.IDENTITY)
   protected int id; ... 
}

public class TaPatternInstrument extends BaseEntity {

   @ManyToOne(fetch = FetchType.EAGER)
   @JoinColumn(name = "instrument_id", nullable = false, foreignKey = @ForeignKey(name = 
            "tapatterninstrument_instrument_fk"))
   private Instrument instrument;

}


public class Instrument extends BaseEntity {

  @OneToMany(mappedBy = "instrument", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
  private List<InstrumentPrice> instrumentPriceList;

}

生成的 SQL

SELECT DISTINCT tapatterni0_.id  AS id1_34_0_, 
...
FROM   tapatterninstrument tapatterni0_ 
       LEFT OUTER JOIN instrument instrument1_ 
                    ON tapatterni0_.instrument_id = instrument1_.id 
       LEFT OUTER JOIN instrumentprice instrument2_ 
                    ON instrument1_.id = instrument2_.instrument_id 
WHERE  tapatterni0_.id = ? 
       AND ( instrument2_.date BETWEEN ? AND ? ) 

【问题讨论】:

  • 您是否尝试过使用任何其他类型的日期?此问题是否仅与 LocalDate 相关?
  • 你的数据集是什么?或者一个例子?另外,您是否在带有本机 sql 的 DMBS 中使用此查询来检查查询是否正常?
  • 我必须使用 LocalDate 因为在实体 InstrumentPrice 中,属性日期定义如下:private LocalDate date;
  • 我在贴出问题的末尾添加了生成的sql。
  • 它给出的错误是什么?堆栈跟踪?

标签: java spring hibernate join where-clause


【解决方案1】:

解决方案是在 instrumentPriceList 上添加一个 FETCH:LEFT JOIN 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 的额外调用,不再考虑 where 子句,从而从实体 instrumentPrice 检索所有记录。

【讨论】:

    猜你喜欢
    • 2014-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    相关资源
    最近更新 更多