java - JPA/hibernate : How to pass value in @where clause

标签 java hibernate jpa annotations persistence

我想知道是否可以使用 JPA 注释配置动态 @where 子句。假设我有两个类之间的关系 - 公司(父级)和调用(子级)。

如果有这样的Annotation声明(伪代码)。

@ManyToOne
@JoinColumn(name = "ID_MEN_PRE", referencedColumnName = "ID_MEN"
@Where(clause="invoice_month=:month")
private Testinvoices invoice;

我现在想做的是在 where 子句中传递“月”值。结果应仅返回给定日期的 Testinvoices。

有什么办法吗?

最佳答案

不可以,但是过滤器 - 见下文 - 可以参数化。

但是在我看来,您似乎在尝试建立关系模型,而实际上查询是更好的解决方案。

或者,在数据库级别创建按日期过滤的 View ,并将发票映射到该 View 而不是表格。

17.1。 hibernate 过滤器

Hibernate3 has the ability to pre-define filter criteria and attach those filters at both a class level and a collection level. A filter criteria allows you to define a restriction clause similar to the existing "where" attribute available on the class and various collection elements. These filter conditions, however, can be parameterized. The application can then decide at runtime whether certain filters should be enabled and what their parameter values should be. Filters can be used like database views, but they are parameterized inside the application.

关于java - JPA/hibernate : How to pass value in @where clause,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19377467/

相关文章:

java - CrudRepository 和 hibernate : save(List<S>) vs save(Entity) in transaction

java - Apache James 安装问题 - java.lang.UnsupportedClassVersionError :

java - 使用 JPA 保留 Map<Integer,Float>

java - 记录在顺序数据库操作之间似乎被锁定(Spring Data、Hibernate)

hibernate - PostgreSQL 中 from dual 的等价性

java - Spring Boot JPA CrudRepository 如何设置可选搜索参数

java - 如何正确导入类: Http Client from Apache Commons

java - 在没有列表/数组的情况下跟踪猜谜游戏中的最低分数 (Java)

java - Hibernate 过滤器关联

java - JPA 标准 API 函数转换或转换