简体   繁体   English

Hibernate Dynamic @Where子句

[英]Hibernate Dynamic @Where clause

I am having @Where at @Table level. 我在@Table级别有@Where。

I need to get rows with EffDate field less than current date. 我需要获取EffDate字段少于当前日期的行。

@Entity
@Table(name="party")
@Where(clause="EffDate < " + currentdate())
public class Party implements Serializable {
.......
}

I understand @Where takes constant expression which needs to be provided on compile time. 我知道@Where接受需要在编译时提供的常量表达式。 But I need to compare with current date from Java and not from Database. 但是我需要与Java而不是数据库的当前日期进行比较。

Any Idea how can I do that. 任何想法我该怎么做。

您可以为当前时间戳使用SQL函数/常量(取决于数据库的类型,例如current_timestamp )(但是请注意,由于这将是纯SQL,因此它将使用数据库服务器的时间戳,而不是数据库的时间戳)。 Java应用程序在其上运行的服务器:

@Where(clause="EffDate < current_timestamp") 

Write a Hibernate filter and enable it by default for all Sessions in the application. 编写一个休眠过滤器,并默认为应用程序中的所有会话启用它。 Depending on which framework you use for transaction management, you can add an interceptor/hook that enables the filter when transactions are started. 根据用于事务管理的框架,可以添加拦截器/挂钩,以在启动事务时启用筛选器。

@Where annotation will not work, since it accepts constant expressions. @Where注释不起作用,因为它接受常量表达式。 Instead, I use findOnByEffectiveDateLessThanEquals from JPARepository. 相反,我使用来自JPARepository的findOnByEffectiveDateLessThanEquals。

http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM