Hibernate 限制:两个 DB.table.propertyName 之间的对象

发布于 2024-12-08 18:36:16 字数 1231 浏览 1 评论 0原文

这是Hibernate之间约束的正常使用

Criterion critDate = Restrictions.between("fromDatePropName", model.getFromDate(), model.getToDate()) ;

但我想要在hibernate中具有两个或多个propertyNameObject之间的约束。

例如:我想要 tableName.fromDatePropNametableName.toDatePropName 之间的日期 '2011-10-01'

这就是我想要做的使用一种方法:

Ex:

Criterion critDate = Restrictions.between(model.getDate(), "fromDatePropName", "toDatePropName") ;

Visual Ex:

这是生成的 SQL:

Criterion critDate = Restrictions.between("fromDatePropName", model.getFromDate(), model.getToDate()) ;
Criterion critDate = Restrictions.between(  "toDatePropName", model.getFromDate(), model.getToDate()) ;

...
AND tableName.fromDatePropName  between DATE '2011-10-01' and DATE '2011-10-31' 
AND tableName.toDatePropName    between DATE '2011-10-01' and DATE '2011-10-31' 
...

但这是我想要生成的 SQL:

Criterion critDate = Restrictions.between(model.getDate(), "fromDatePropName", "toDatePropName") ;

...
AND DATE'2011-10-01'  between tableName.fromDatePropName and tableName.toDatePropName
...

感谢您的回答。

This is the normal use of Hibernate's between constraint

Criterion critDate = Restrictions.between("fromDatePropName", model.getFromDate(), model.getToDate()) ;

But I want the constraint between Object with two or more propertyName in hibernate.

For ex: I want the Date '2011-10-01' between tableName.fromDatePropName and tableName.toDatePropName

This is how I want to do with one method:

Ex:

Criterion critDate = Restrictions.between(model.getDate(), "fromDatePropName", "toDatePropName") ;

Visual Ex:

Here is the SQL generated with:

Criterion critDate = Restrictions.between("fromDatePropName", model.getFromDate(), model.getToDate()) ;
Criterion critDate = Restrictions.between(  "toDatePropName", model.getFromDate(), model.getToDate()) ;

...
AND tableName.fromDatePropName  between DATE '2011-10-01' and DATE '2011-10-31' 
AND tableName.toDatePropName    between DATE '2011-10-01' and DATE '2011-10-31' 
...

But this is the SQL I want to generate:

Criterion critDate = Restrictions.between(model.getDate(), "fromDatePropName", "toDatePropName") ;

...
AND DATE'2011-10-01'  between tableName.fromDatePropName and tableName.toDatePropName
...

Thanks for your answers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉宸 2024-12-15 18:36:16

难道不应该是:

Restrictions. Between("date", model.getFromDate(), model.getToDate())

来自 Restrictions. Between() javadoc,属性名称是第一个参数,并且是相对于实体 Criteria是植根于.

如果您确实想指定限制,例如要求根实体的属性位于同一实体的其他两个属性之间,则可以使用 Restrictions.geProperty()Restrictions.leProperty() 方法而不是 Restrictions. Between()

Restrictions.geProperty("date", "fromDate")

Restrictions.leProperty("date", "toDate")

Shouldn't that be:

Restrictions.between("date", model.getFromDate(), model.getToDate())

From the Restrictions.between() javadoc, the property name is the first argument, and is relative to the entity the Criteria is rooted to.

If you really want to specify a restriction such as requiring a property of the rooted entity being between two other properties of the same entity, you could use the Restrictions.geProperty() and Restrictions.leProperty() methods instead of Restrictions.between().

Restrictions.geProperty("date", "fromDate")

and

Restrictions.leProperty("date", "toDate")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文