Spring boot 与 oneToMany 关系,问题双对象

问题描述 投票:0回答:1

我的 Spring Boot 项目有一个小问题,为了向您展示我的问题,我创建了第二个项目。所以我有两门课程,其中一门是关于 OneToMany 与另一门的关系。我的两个班

@Entity
@Table(name = "test2")
@Getter
@Setter
@ToString
public class Test2 {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Fetch(FetchMode.SUBSELECT)
    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "relation_id", referencedColumnName = "id")
    private List<Test3> test3 = new ArrayList<>();
    
}

还有另一个

@Entity
@Table(name = "test3")
@Getter
@Setter
@ToString
public class Test3 {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id3;

    private String test2;
    
}

结果:

[
    {
        "id": 1,
        "test3": [
            {
                "id3": 1,
                "test2": "0"
            }
        ]
    },
    {
        "id": 2,
        "test3": [
            {
                "id3": 2,
                "test2": "0"
            },
            {
                "id3": 3,
                "test2": "1"
            },
            {
                "id3": 4,
                "test2": "3"
            }
        ]
    },
    {
        "id": 2,
        "test3": [
            {
                "id3": 2,
                "test2": "0"
            },
            {
                "id3": 3,
                "test2": "1"
            },
            {
                "id3": 4,
                "test2": "3"
            }
        ]
    },
    {
        "id": 2,
        "test3": [
            {
                "id3": 2,
                "test2": "0"
            },
            {
                "id3": 3,
                "test2": "1"
            },
            {
                "id3": 4,
                "test2": "3"
            }
        ]
    },
    {
        "id": 3,
        "test3": [
            {
                "id3": 5,
                "test2": "11"
            }
        ]
    }
]

预期的结果:

[
    {
        "id": 1,
        "test3": [
            {
                "id3": 1,
                "test2": "0"
            }
        ]
    },
    {
        "id": 2,
        "test3": [
            {
                "id3": 2,
                "test2": "0"
            },
            {
                "id3": 3,
                "test2": "1"
            },
            {
                "id3": 4,
                "test2": "3"
            }
        ]
    },
    {
        "id": 3,
        "test3": [
            {
                "id3": 5,
                "test2": "11"
            }
        ]
    }
]

问题是,如果对象 Test2 在列表中获得了 Test3 的 n 个元素,我在返回 API 时获得了 Test2 的 N 个实体,我该如何解决这个问题?控制器:

public interface TestRepo extends JpaRepository<Test2, Long> {

    @Query(value = "SELECT * FROM test2 t2 JOIN test3 t3 ON t2.id = t3.relation_id", nativeQuery = true)
    List<Test2> getTest2();
    
}

和存储库

@RestController
public class TestContro {
    Logger logger = Logger.getLogger(TestContro.class.getName());

    @Autowired
    TestRepo testRepo;

    @GetMapping("/get")
    ResponseEntity<?> findAllTest() {
        return new ResponseEntity<>(testRepo.getTest2(), HttpStatus.OK);
    }
}

感谢您的帮助:)

java spring-boot duplicates one-to-many
1个回答
0
投票

您的查询将返回笛卡尔积。

  1. 您可以删除
    @Fetch(FetchMode.SUBSELECT)
    并使用 hql 查询
    @Query("select t2 from Test2 t2 join fetch t2.test3")
    与您的 repo 方法
    List<Test2> getTest2();

  1. 您可以保留
    @Fetch(FetchMode.SUBSELECT)
    ,删除您的 repo 方法并在
    testRepo.findAll()
    方法中使用
    testRepo.getTest2()
    代替
    findAllTest()
© www.soinside.com 2019 - 2024. All rights reserved.