Hibernate 条件无法检索连接的值
Hibernate criteria cannot retrieve values of a join
我有以下实体,需要检索特定组中所有商店的名称列表,这些商店在特定城市有分支机构。我发现的大多数教程和文章都与创建这种类型的关系有关,但 none 其中与检索有关!
我多次更改标准,但 Hibernate 每次都显示不同的错误。代码的注释部分是我试过的,各自抛出的异常也都写在前面。
实体
@Entity
public class Store {
@Id
String id;
String name;
@JoinTable(name = "store_groups", joinColumns = { @JoinColumn(name = "id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "code", nullable = false, updatable = false) })
private Set<Group> groups = new HashSet<Group>(0);
private Set<StoreAddress> storeAddresses = new HashSet<StoreAddress>(0);
....
}
@Entity
public class Group {
@Id
String code;
@Column(nullable = false, unique = true)
String name;
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "groups")
Set<Store> storees = new HashSet<Store>(0);
}
@Entity
@Table(name = "StoreAddresses")
@AssociationOverrides({
@AssociationOverride(name = "muJoinTable.store", joinColumns = @JoinColumn(name = "id", nullable = false)),
@AssociationOverride(name = "myJoinTable.city", joinColumns = @JoinColumn(name = "cityCode", nullable = false)) })
public class StoreAddress {
@EmbeddedId
private StoreCitysId myJoinTable = new StoreCitysId();
...
}
@Embeddable
public class StoreCitysId {
@ManyToOne
private Store store;
@ManyToOne
private City city;
}
@Entity
public class City {
@Id
short code;
@Column(nullable = false)
String name;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "myJoinTable.city")
private Set<StoreAddress> storeAddresses = new HashSet<StoreAddress>(
0);
}
条件
List<String> storees = (List<String>) sessionFactory
.getCurrentSession()
.createCriteria(Store.class)
.setProjection(
Projections.property("name").as(
"storeName"))
.createAlias("groups", "group")
.createAlias("storeAddresses", "address")
// .createAlias("address.myJoinTable.city", "city")
// .createAlias("address.myJoinTable", "myJoinTable")
// .createAlias("myJoinTable.city", "city") Error: Criteria objects cannot be created directly on components
.setFetchMode("group", FetchMode.JOIN)
.add(Restrictions.ilike("group.code", store))
.add(Restrictions.eq("address.myJoinTable.cityCode",
1)).list(); //city.code -> Error: could not resolve property: cityCode of:com.example.entity.StoreAddress address.myJoinTable.cityCode could not resolve property: myJoinTable.cityCode of:com.example.entity.StoreAddress
您的标准 Restrictions.eq("address.myJoinTable.cityCode", 1)
没有引用 属性 而是列的名称。您可以改为使用 address.myJoinTable.city
并将值设置为 session.load(City.class, 1)
,从而使 Restrictions.eq("address.myJoinTable.city", session.load(City.class, 1))
还有这个:
.createAlias("address.myJoinTable", "myJoinTable")
.createAlias("myJoinTable.city", "city")
应该是:
.createAlias("address.myJoinTable.city", "city")
我有以下实体,需要检索特定组中所有商店的名称列表,这些商店在特定城市有分支机构。我发现的大多数教程和文章都与创建这种类型的关系有关,但 none 其中与检索有关!
我多次更改标准,但 Hibernate 每次都显示不同的错误。代码的注释部分是我试过的,各自抛出的异常也都写在前面。
实体
@Entity
public class Store {
@Id
String id;
String name;
@JoinTable(name = "store_groups", joinColumns = { @JoinColumn(name = "id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "code", nullable = false, updatable = false) })
private Set<Group> groups = new HashSet<Group>(0);
private Set<StoreAddress> storeAddresses = new HashSet<StoreAddress>(0);
....
}
@Entity
public class Group {
@Id
String code;
@Column(nullable = false, unique = true)
String name;
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "groups")
Set<Store> storees = new HashSet<Store>(0);
}
@Entity
@Table(name = "StoreAddresses")
@AssociationOverrides({
@AssociationOverride(name = "muJoinTable.store", joinColumns = @JoinColumn(name = "id", nullable = false)),
@AssociationOverride(name = "myJoinTable.city", joinColumns = @JoinColumn(name = "cityCode", nullable = false)) })
public class StoreAddress {
@EmbeddedId
private StoreCitysId myJoinTable = new StoreCitysId();
...
}
@Embeddable
public class StoreCitysId {
@ManyToOne
private Store store;
@ManyToOne
private City city;
}
@Entity
public class City {
@Id
short code;
@Column(nullable = false)
String name;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "myJoinTable.city")
private Set<StoreAddress> storeAddresses = new HashSet<StoreAddress>(
0);
}
条件
List<String> storees = (List<String>) sessionFactory
.getCurrentSession()
.createCriteria(Store.class)
.setProjection(
Projections.property("name").as(
"storeName"))
.createAlias("groups", "group")
.createAlias("storeAddresses", "address")
// .createAlias("address.myJoinTable.city", "city")
// .createAlias("address.myJoinTable", "myJoinTable")
// .createAlias("myJoinTable.city", "city") Error: Criteria objects cannot be created directly on components
.setFetchMode("group", FetchMode.JOIN)
.add(Restrictions.ilike("group.code", store))
.add(Restrictions.eq("address.myJoinTable.cityCode",
1)).list(); //city.code -> Error: could not resolve property: cityCode of:com.example.entity.StoreAddress address.myJoinTable.cityCode could not resolve property: myJoinTable.cityCode of:com.example.entity.StoreAddress
您的标准 Restrictions.eq("address.myJoinTable.cityCode", 1)
没有引用 属性 而是列的名称。您可以改为使用 address.myJoinTable.city
并将值设置为 session.load(City.class, 1)
,从而使 Restrictions.eq("address.myJoinTable.city", session.load(City.class, 1))
还有这个:
.createAlias("address.myJoinTable", "myJoinTable")
.createAlias("myJoinTable.city", "city")
应该是:
.createAlias("address.myJoinTable.city", "city")