如何在Spring Boot中通过检查嵌套对象来获取对象?

时间:2019-07-02 16:33:24

标签: java mysql spring spring-boot

使用JpaRepository无法获得我想要的东西。我将尝试使用以下代码来解释我想要的东西:

存储库

 @Repository
public interface CompanyRepository extends JpaRepository<Company, Long> {

    Optional<Company> findByIdAndBranches_parent_idIsNull(Long id);
}

服务

@Service
public class BillService {

@Autowired
private CompanyRepository companyRepository;

@Autowired
private BranchRepository branchRepository;

public Company getCompanyById(Long id)
{
    Optional<Company> company = companyRepository.findByIdAndBranches_parent_idIsNull(id);
    return company.get();
}

公司实体类

public class Company 
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String name;

private String address;

@OneToMany(mappedBy = "company")
private List<Branch> branches;

@OneToMany(mappedBy = "company")
private List<User> users;

}

分支实体类

@Entity
public class Branch 
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String name;

private String address;

@JsonIgnore
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "company_id")
private Company company;

@JsonIgnore
@ManyToOne(cascade = CascadeType.ALL )
@JoinColumn(name = "parent_id")
@NotFound(action = NotFoundAction.IGNORE)
private Branch parent;

@OneToMany(mappedBy = "parent")
@NotFound(action = NotFoundAction.IGNORE)
private List<Branch> subBranches;

@OneToMany(mappedBy = "branch")
private List<User> users;

}

现在我要获得的是分支机构的父ID为NULL的公司,但无论父ID如何,我都将获得所有分支

这就是我想要的结果

{
"id": 1,
"name": "Lakshya",
"address": "Bahadurgarh",
"branches": [
    {
        "id": 1,
        "name": "Lakshya Branch1",
        "address": "Bahadurgarh1",
        "subBranches": [
            {
                "id": 3,
                "name": "Lakshya Branch1_3",
                "address": "Bahadurgarh1_3",
                "subBranches": [],
                "users": []
            }
        ],
        "users": [
            {
                "id": 3,
                "name": "User3_Company1_Branch1",
                "address": "Bgz"
            }
        ]
    }
],
"users": [
    {
        "id": 1,
        "name": "User_Company1",
        "address": "Bgz"
    },
    {
        "id": 2,
        "name": "User_Company1_Branch1",
        "address": "Bgz"
    },
    {
        "id": 3,
        "name": "User3_Company1_Branch1",
        "address": "Bgz"
    }
]
}

但这就是所得到的

{
"id": 1,
"name": "Lakshya",
"address": "Bahadurgarh",
"branches": [
    {
        "id": 1,
        "name": "Lakshya Branch1",
        "address": "Bahadurgarh1",
        "subBranches": [
            {
                "id": 3,
                "name": "Lakshya Branch1_3",
                "address": "Bahadurgarh1_3",
                "subBranches": [],
                "users": []
            }
        ],
        "users": [
            {
                "id": 3,
                "name": "User3_Company1_Branch1",
                "address": "Bgz"
            }
        ]
    },
    {
        "id": 3,
        "name": "Lakshya Branch1_3",
        "address": "Bahadurgarh1_3",
        "subBranches": [],
        "users": []
    }
],
"users": [
    {
        "id": 1,
        "name": "User_Company1",
        "address": "Bgz"
    },
    {
        "id": 2,
        "name": "User_Company1_Branch1",
        "address": "Bgz"
    },
    {
        "id": 3,
        "name": "User3_Company1_Branch1",
        "address": "Bgz"
    }
]

}

1 个答案:

答案 0 :(得分:1)

如果需要过滤掉关联的实体,可以使用@Where。它不是JPA规范的一部分,但是Hibernate的JPA实现提供了此注释。因此,您可以指定任何其他条件:

public class Company 
{
    ...

    @OneToMany(mappedBy = "company")
    @Where(clause = "parent_id is null")
    private List<Branch> branches;

    @OneToMany(mappedBy = "company")
    private List<User> users;
}

然后,由于不再需要额外的条件,因此您必须更改存储库的方法名称:

@Repository
public interface CompanyRepository extends JpaRepository<Company, Long> {
    Optional<Company> findById(Long id);
}

使用这种方法,您总会得到只有分支机构且没有母公司的公司。如果您需要获得公司的所有分支机构(无论它们是否具有母公司),都可以为Branch实体类创建和使用存储库。