简体   繁体   English

休眠条件无法检索联接的值

[英]Hibernate criteria cannot retrieve values of a join

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. 我多次更改了标准,但是Hibernate每次都显示不同的错误。 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

Your criterion Restrictions.eq("address.myJoinTable.cityCode", 1) doesn't reference a property but the name of the column. 您的条件Restrictions.eq("address.myJoinTable.cityCode", 1)不引用属性,而是引用列的名称。 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)) 您可以改用address.myJoinTable.city并将值设置为session.load(City.class, 1)从而使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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM