我有一个房子,可以有几个单位,每个单位每月可以有一个水入口。 Water
对象包含Flat
的引用内容。 Flat
对象包含House
的引用。我想根据当前月份和房屋从数据库中选择所有条目。我试过这样做:
public static List<Water> getAll(Date date, House house){
List<Water> waterList = null;
Calendar myCalendar = Calendar.getInstance();
myCalendar.setTime(date);
myCalendar.set(Calendar.DAY_OF_MONTH, 1);
Date monthStart = new java.sql.Date(myCalendar.getTimeInMillis());
myCalendar.add(Calendar.DAY_OF_MONTH,
(myCalendar.getMaximum(Calendar.DAY_OF_MONTH) - myCalendar.get(Calendar.DAY_OF_MONTH)));
Date monthEnd = new java.sql.Date(myCalendar.getTimeInMillis());
Session sess = mainApp.getSessionFactory().openSession();
Transaction tx = null;
try {
tx = sess.beginTransaction();
// create criteria builder
CriteriaBuilder builder = sess.getCriteriaBuilder();
// create criteria
CriteriaQuery<Water> query = builder.createQuery(Water.class);
// specify criteria root
Root<Water> root = query.from(Water.class);
query.select(root)
.where(builder.and(builder.equal(root.get("house"), house),
builder.greaterThanOrEqualTo(root.get("date"), monthStart),
builder.lessThanOrEqualTo(root.get("date"), monthEnd)));
waterList = sess.createQuery(query).getResultList();
tx.commit();
} catch (Exception e) {
if (tx != null)
tx.rollback();
e.printStackTrace();
} finally {
sess.close();
}
return waterList;
}
但我收到了一个错误:
java.lang.IllegalArgumentException: Unable to locate Attribute with the the given name [house] on this ManagedType [home.accounting.model.Water]
据我所知,这是因为Water
对象没有直接引用house。
我应该如何修改Criteria Query
,以便根据房屋和日期选择Water
表格中的所有记录?
答案 0 :(得分:3)
正如你在模式中提到的那样,Water提到平面和平面都提到了房子,你可以先从屋里取出房子,然后从平面上取房子。
protected _set<K extends keyof IFilter>(key: K, value: IFilter[K], reset?: true) {
this.filter[key] = value;
}
this._set("category", 0) // ok
this._set("category", "0") // error category is not string