Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.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
Hibernate @SpringBoot 2.1.5.RELEASE中的OneToMany Spring数据JPA无法获取所有数据_Hibernate_Spring Boot_Jpa_Orm_Spring Data Jpa - Fatal编程技术网

Hibernate @SpringBoot 2.1.5.RELEASE中的OneToMany Spring数据JPA无法获取所有数据

Hibernate @SpringBoot 2.1.5.RELEASE中的OneToMany Spring数据JPA无法获取所有数据,hibernate,spring-boot,jpa,orm,spring-data-jpa,Hibernate,Spring Boot,Jpa,Orm,Spring Data Jpa,我有一个基本的SpringBoot 2.1.5.RELEASE应用程序。使用Spring初始值设定项、JPA、嵌入式Tomcat、Thymeleaf模板引擎,并将包作为可执行JAR文件 我有这个域类: @Entity @Table(name="t_purchase") @JsonInclude(JsonInclude.Include.NON_NULL) public class Purchase implements Serializable { public Purchase() {

我有一个基本的SpringBoot 2.1.5.RELEASE应用程序。使用Spring初始值设定项、JPA、嵌入式Tomcat、Thymeleaf模板引擎,并将包作为可执行JAR文件

我有这个域类:

@Entity
@Table(name="t_purchase")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Purchase implements Serializable {

    public Purchase() {
    }

    public Purchase(Shop shop) {
        super();
        this.shop = shop;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonProperty("id")
    private Long id;    

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = “shop_id")
    @JsonIgnore
    Shop shop;
…
}
而且

@Entity
@Table(name = “t_shop")
public class Shop implements Serializable {


    public Shop(String name) {
        this.name = name;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonProperty("id")
    private Long id;

    @JsonProperty("name")
    private String name;

    @OneToMany(mappedBy = “shop", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
    @JsonIgnore
    private Set<Purchase> purchases = new HashSet<Purchase>();

        …
}
然后我创建了这个Junit方法:

@Test
public void testFindByShopIdWithPurchases () {

    Shop shop = new Shop ("Shop_NAME");

    shopService.save(shop);

    Purchase purchase1 = new Purchase(shop);
    Purchase purchase2 = new Purchase(shop);

    shop.getPurchases().add(purchase1);
    shop.getPurchases().add(purchase2);

    shopService.save(shop);

    Shop shopWithPurchases = shopService.findByShopIdWithPurchases(shop.getId());

    assertEquals (2, shopWithPurchases.getPurchases().size());


}   

但是它失败了,因为它返回1次购买,而不是2次购买。如果您为具有生成id的实体重写equals,则需要处理两个id都为null的情况。否则,哈希集将把新的实体视为相等的,并且只存储一个

在这种情况下,可以使用默认的等于:

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Purchase  other = (Purchase) obj;
    if(this.id == null) {
        if(other.id == null) {
            return super.equals(other);
        }
        return false;
    }
    return this.id.equals(other.id);
}

如果为具有生成id的实体重写equals,则需要处理两个id都为null的情况。否则,哈希集将把新的实体视为相等的,并且只存储一个

在这种情况下,可以使用默认的等于:

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Purchase  other = (Purchase) obj;
    if(this.id == null) {
        if(other.id == null) {
            return super.equals(other);
        }
        return false;
    }
    return this.id.equals(other.id);
}