我有3个实体 - Storage
,Item
和Relation
。 Storage
有几个Item
个实体,项目由Relation
个实体绑定。关系可以绑定来自不同存储的项目。为简化起见,我想通过查询加载关系并希望快速完成。所以现在我有3个查询 - 加载Storage
,加载存储和加载关系(List<Relation> relations
字段)下的所有项目。
现在我想告诉hibernate如何加载Collection<Relation> extRelation
字段。我尝试了@Formula
,@CalculatedColumn
以及@ManyToMany
和@JoinFormula
的不同组合。但生成的查询是错误的(或忽略我的查询)。另外,由于https://hibernate.atlassian.net/browse/HHH-9897错误,我无法使用@OneToMany
。
最新例外是:
引起:org.h2.jdbc.JdbcSQLException:表 未找到“TEST_STORAGES_TEST_RELATIONS”; SQL语句:
SELECT extrelatio0_.test_storages_storage_id AS test_sto1_2_0_
,extrelatio0_.extRelation_from_item_id AS extRelat2_3_0_
,extrelatio0_.extRelation_to_item_id AS extRelat3_3_0_
,relation1_.from_item_id AS from_ite1_1_1_
,relation1_.to_item_id AS to_item_2_1_1_
,relation1_.relation_id AS relation3_1_1_
,relation1_.storage_id AS storage_4_1_1_
FROM test_storages_test_relations extrelatio0_
INNER JOIN test_relations relation1_ ON extrelatio0_.extRelation_from_item_id = relation1_.from_item_id
AND extrelatio0_.extRelation_to_item_id = relation1_.to_item_id
WHERE extrelatio0_.test_storages_storage_id = ?
我的实体:
@Entity
@Table(name = "test_storages")
public class Storage {
@Id
@Column(name = "storage_id")
private BigInteger storageId;
private String name;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true, targetEntity = Item.class)
@JoinColumn(name = "storage_id", updatable = false)
@MapKey
private List<Item> items;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true, targetEntity = Relation.class)
@JoinColumn(name = "storage_id", updatable = false)
@Fetch(org.hibernate.annotations.FetchMode.SELECT)
private List<Relation> relations;
@ManyToMany()
@JoinColumnsOrFormulas({
@JoinColumnOrFormula(formula =
@JoinFormula(
value = "(select dep.from_item_id, dep.to_item_id from test_relations dep where dep.storage_id = ?)"
)
)
})
private Collection<Relation> extRelation;
}
@Entity
@Table(name = "test_items")
public class Item {
@Id
@Column(name = "item_id")
private BigInteger itemId;
private String name;
@Column(name = "storage_id")
private BigInteger storageId;
}
@Entity
@Table(name = "test_relations")
public class Relation {
@Column(name = "relation_id")
private BigInteger relationId;
@Column(name = "storage_id")
private BigInteger storageId;
@EmbeddedId
private RelationPK pk;
}
@Embeddable
public class RelationPK implements Serializable {
@Column(name = "from_item_id")
private BigInteger fromItemId;
@Column(name = "to_item_id")
private BigInteger toItemId;
}
上提供的所有来源