Reputation: 2923
I have following entities and need to retrieve a list of names of all stores that are in a specific group and have branches in a specific city. Majority of tutorials and articles that Ive found are related to creating this type of relationships but none of them is about retrieval!
I changed the criteria for many times but Hibernate shows different errors for each. The commented parts of the code are those that I tried and the respective thrown exception is also written in front of each.
Entities
@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);
}
Criteria
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
Upvotes: 1
Views: 425
Reputation: 4190
Your criterion Restrictions.eq("address.myJoinTable.cityCode", 1)
doesn't reference a property but the name of the column. You could instead use address.myJoinTable.city
and set the value to session.load(City.class, 1)
making Restrictions.eq("address.myJoinTable.city", session.load(City.class, 1))
And this:
.createAlias("address.myJoinTable", "myJoinTable")
.createAlias("myJoinTable.city", "city")
Should be:
.createAlias("address.myJoinTable.city", "city")
Upvotes: 1