我是Hibernate的新手,并且一直在努力解决hibernate问题。我得到org.Hibernate.QueryException: not an association: id
。
这是正确运行的查询,我正在尝试实现。可以说我有带架子的房间。每个方框都包含一个项目。我只有盒子的条形码,我想得到那个房间里的所有物品。
select * from items where location_id in
(select id from storage where parent_id in
(select id from storage where parent_id in
(select id from storage where barcode like 'some_box_barcode_here')))
这就是我在JPA中组织它的方式(我得到了例外):
DetachedCriteria subCriteriaRoom = DetachedCriteria.forClass(Storage.class, "storageRoom")
.createAlias("id", "roomId")
.createAlias("index", "roomIndex")
.add(Restrictions.eq("roomIndex", barcode));
subCriteriaRoom.setProjection(Projections.property("roomId"));
DetachedCriteria subCriteriaShelf = DetachedCriteria.forClass(Storage.class, "storageShelf")
.createAlias("id", "shelfId")
.createAlias("parent", "shelfParent")
.add(Restrictions.in("shelfParent", subCriteriaRoom));
subCriteriaShelf.setProjection(Projections.property("shelfId"));
DetachedCriteria subCriteriaBox = DetachedCriteria.forClass(Storage.class, "storageBox")
.createAlias("id", "boxId")
.createAlias("parent", "boxParent")
.add(Restrictions.in("boxParent", subCriteriaShelf));
subCriteriaBox.setProjection(Projections.property("boxId"));
Criteria criteria = session.createCriteria(Item.class, "item");
criteria.createAlias("storage", "storage")
.add(Subqueries.propertyEq("location", subCriteriaBox));
List list = criteria.list();
有关表格的一些相关信息:
1
@Entity
@Table(name = "storage")
public class Storage implements DataModel {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "sequence")
private long id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "parent_id")
private Storage parent;
@Column(name = "barcode", length = 31, nullable = true)
private String barcode;
2
@Entity
@Table(name = "items")
public class Items implements DataModel {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "sequence")
private long id;
@OneToOne(fetch = FetchType.EAGER) //the foreign key to storage.id
@JoinColumn(name = "location_id")
private Storage location;
任何提示都会非常感激。
P.S。我需要这么大的查询的原因是,现在房间在数据库中成倍增加,因为一些错误将暂时无法修复。