Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Hibernate条件无法检索联接的值_Java_Mysql_Hibernate_Hibernate Criteria - Fatal编程技术网

Java Hibernate条件无法检索联接的值

Java Hibernate条件无法检索联接的值,java,mysql,hibernate,hibernate-criteria,Java,Mysql,Hibernate,Hibernate Criteria,我有以下实体,需要检索特定组中且在特定城市有分支机构的所有商店的名称列表。我发现的大多数教程和文章都与创建这种类型的关系有关,但没有一篇是关于检索的 我多次更改了标准,但Hibernate为每个标准显示了不同的错误。代码的注释部分是我尝试过的部分,相应的抛出异常也写在每个代码前面 实体 @Entity public class Store { @Id String id; String name; @JoinTable(name = "store_groups",

我有以下实体,需要检索特定组中且在特定城市有分支机构的所有商店的名称列表。我发现的大多数教程和文章都与创建这种类型的关系有关,但没有一篇是关于检索的

我多次更改了标准,但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);
}
@实体
公共类商店{
@身份证
字符串id;
字符串名;
@JoinTable(name=“store_groups”,joinColumns={@JoinColumn(name=“id”,nullable=false,updateable=false)},inverseJoinColumns={@JoinColumn(name=“code”,nullable=false,updateable=false)})
私有集组=新哈希集(0);
私有集storeAddresses=新哈希集(0);
....
}
@实体
公共课组{
@身份证
字符串代码;
@列(nullable=false,unique=true)
字符串名;
@ManyToMany(fetch=FetchType.LAZY,mappedBy=“groups”)
Set storees=新哈希集(0);
}
@实体
@表(name=“StoreAddresses”)
@联想超越({
@AssociationOverride(name=“muJoinTable.store”,joinColumns=@JoinColumn(name=“id”,nullable=false)),
@AssociationOverride(name=“myJoinTable.city”,joinColumns=@JoinColumn(name=“cityCode”,nullable=false)))
公共类存储地址{
@嵌入ID
private StoreCitysId myJoinTable=新的StoreCitysId();
...
}
@可嵌入
公共类StoreCitysId{
@许多酮
私人店铺;
@许多酮
私人城市;
}
@实体
公营城市{
@身份证
短代码;
@列(nullable=false)
字符串名;
@OneToMany(fetch=FetchType.LAZY,mappedBy=“myJoinTable.city”)
私有集storeAddresses=新哈希集(
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
List storees=(List)sessionFactory
.getCurrentSession()
.createCriteria(Store.class)
.setProjection(
物业(“名称”)。作为(
“店名”))
.createAlias(“组”、“组”)
.createAlias(“存储地址”、“地址”)
//.createAlias(“address.myJoinTable.city”,“city”)
//.createAlias(“address.myJoinTable”,“myJoinTable”)
//.createAlias(“myJoinTable.city”、“city”)错误:无法直接在组件上创建条件对象
.setFetchMode(“组”,FetchMode.JOIN)
.add(Restrictions.ilike(“group.code”,store))
.add(Restrictions.eq(“address.myJoinTable.cityCode”),
1) )。列表()//city.code->错误:无法解析属性:cityCode of:com.example.entity.StoreAddress.myJoinTable.cityCode无法解析属性:myJoinTable.cityCode of:com.example.entity.StoreAddress

您的标准
限制。eq(“address.myJoinTable.cityCode”,1)
不引用属性,而是引用列的名称。您可以改为使用
address.myJoinTable.city
并将值设置为
session.load(city.class,1)
making
Restrictions.eq(“address.myJoinTable.city”,session.load(city.class,1))

这是:

.createAlias("address.myJoinTable", "myJoinTable")
.createAlias("myJoinTable.city", "city")
应该是:

.createAlias("address.myJoinTable.city", "city")