Hibernate HQL Map查询

时间:2017-09-15 11:16:22

标签: java hibernate hql

我有一个班级:

@Entity
@Table(name = "days")
public class DayEntry {

@Expose
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
.
.
@Expose
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "day_item_hours")
@MapKeyJoinColumn(name = "item_id")
@Column(name = "hours")
private Map<Item, Double> itemsHours;
.
.
}

我需要从数据库中获取所有 DayEntries ,其中包含 itemHours 字段中的特定

我试过了:

private final String FIND_BY_ACTIVITY = "from DayEntry d where :activity in index(d.itemsHours)";
@Override
public Collection<DayEntry> findByActivity(Item activity) {
    Query query = entityManager.createQuery(FIND_BY_ACTIVITY);
    query.setParameter("activity", activity);
    return query.getResultList();
}

得到这个:

java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ( near line 1, column 75 [from ua.siemens.dbtool.model.timesheet.DayEntry d where :activity in index(d.itemsHours)]] with root cause
 org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ( near line 1, column 75 [from ua.siemens.dbtool.model.timesheet.DayEntry d where :activity in index(d.itemsHours)]

不确定如何处理此问题。 谢谢!

更新

目前我最终得到了纯SQL查询:

    private final String FIND_BY_ACTIVITY = "SELECT * FROM days where id = (SELECT DayEntry_id from day_item_hours where day_item_hours.item_id = :activityId)";

@Override
public Collection<DayEntry> findByActivity(Item activity) {
    Query query = entityManager.createNativeQuery(FIND_BY_ACTIVITY_ID, DayEntry.class);
    query.setParameter("activityId", activity.getId());
    return query.getResultList();
}

但我仍然想知道如何使用HQL。

0 个答案:

没有答案