Hibernate 限制:两个 DB.table.propertyName 之间的对象
这是Hibernate之间约束的正常使用
Criterion critDate = Restrictions.between("fromDatePropName", model.getFromDate(), model.getToDate()) ;
但我想要在hibernate中具有两个或多个propertyName
的Object
之间的约束。
例如:我想要 tableName.fromDatePropName
和 tableName.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
难道不应该是:
Restrictions. Between("date", model.getFromDate(), model.getToDate())
来自
Restrictions. Between()
javadoc,属性名称是第一个参数,并且是相对于实体 Criteria是植根于.如果您确实想指定限制,例如要求根实体的属性位于同一实体的其他两个属性之间,则可以使用
Restrictions.geProperty()
和Restrictions.leProperty()
方法而不是Restrictions. Between()
。和
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()
andRestrictions.leProperty()
methods instead ofRestrictions.between()
.and