【发布时间】:2011-01-20 05:49:59
【问题描述】:
假设 Student 是包含 ComputerScienceStudent 和 ITStudent 的继承层次结构中的父类。 Student 定义了一个名为 favoriteColors 的字段,它是一个值类型的字符串集合。使用条件 API,我想查询所有将“蓝色”列为他们最喜欢的颜色之一的学生。
这里是相关的java sn -p
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
abstract class Student {
@CollectionOfElements
Set<String> favoriteColors = new TreeSet<String>();
我查看了在 http://docs.jboss.org/hibernate/core/3.3/reference/en/html/querycriteria.html 找到的休眠文档,看起来像这样的东西可能是一个开始
List students = sess.createCriteria(Student.class)
.createCriteria("favoriteColors")
.add( Restrictions.eq(??????, "blue") )
.list();
但我不知道要填写什么作为属性的名称,因为 String 不是我定义的类(因此是??????)
我真的很想避免使用 HQL,除非绝对没有办法使用标准来做到这一点。我还想避免添加 sqlRestrictions。我认为示例 API 不会起作用,因为 Student 是继承层次结构中的抽象父类,我无法创建具体的 Student 作为示例传递。
这可能吗?感谢您的宝贵时间!
【问题讨论】:
标签: hibernate