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 子对象中存在重复数据_Hibernate_Spring Boot_Spring Data Jpa - Fatal编程技术网

Hibernate 子对象中存在重复数据

Hibernate 子对象中存在重复数据,hibernate,spring-boot,spring-data-jpa,Hibernate,Spring Boot,Spring Data Jpa,我将SpringBoot2.1与SpringDataJPA和hibernate一起使用 @Entity public class Factories{ @Id @SequenceGenerator(name = "factories_id_seq", sequenceName = "factories_id_seq", allocationSize = 1) @GeneratedValue(strategy = GenerationType.SEQUENCE, gener

我将SpringBoot2.1与SpringDataJPA和hibernate一起使用

@Entity
public class Factories{
    @Id
    @SequenceGenerator(name = "factories_id_seq", sequenceName = "factories_id_seq", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "factories_id_seq")
    private Integer id;

    @OneToMany(mappedBy = "factory", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
    private List<Machines> machines = new ArrayList<>();

    @ElementCollection(fetch = FetchType.LAZY)
    private Set<String> emails = new HashSet<>();

}   
我试着用清晰的和不清晰的

我的工厂id为1

有3台机器和3封电子邮件

我得到的不是3台机器和3封电子邮件,而是9台机器。数据库中只有3个

就像笛卡尔积完成了一样


任何想法

您只需在Hibernate或任何JPA中遇到
MultipleBagFetch
问题。是的,你是对的,你所看到的是笛卡尔积的结果,但这一切照旧。有更多的帖子在讨论同样的问题。要解决您的问题,您有3个选项:

a) 将提取拆分为两个连接查询 b) 使用特定于Hibernate的解决方案: 或

@Fetch是hibernate,热JPA标准:
SELECT distinct f from Factories f "
        + "LEFT JOIN FETCH f.machines "
        + "Left JOIN FETCH f.emails "
        + "Left JOIN FETCH f.cities "
        + "where f.id=:id
SELECT distinct f from Factories f "
        + "LEFT JOIN FETCH f.machines "
        + "Left JOIN FETCH f.cities "
        + "where f.id=:id

SELECT distinct f from Factories f "
        + "LEFT JOIN FETCH f.machines "
        + "Left JOIN FETCH f.emails "
        + "where f.id=:id
@LazyCollection(LazyCollectionOption.FALSE)
@Fetch(value = FetchMode.SUBSELECT)