简体   繁体   English

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

[英]How can i get an object with checks on nested object in Spring boot?

I am unable to to get what i intend using a JpaRepository. 我无法使用JpaRepository得到我想要的东西。 I'll try to explain what I want using the following code: 我将尝试使用以下代码解释我想要的内容:

Repository 知识库

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

    Optional<Company> findByIdAndBranches_parent_idIsNull(Long id);
}

Service 服务

@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();
}

Company Entity class 公司实体类

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;

} }

Branch Entity class 分支实体类

@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;

}

Now what i want to get is company with branches having parent id NULL but am getting all branches irrespective of parent id 现在我想要得到的是分支机构的父ID为NULL但与父ID无关的所有分支

This is what i want as result 这就是我想要的结果

{
"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"
    }
]
}

But this is what am getting 但这就是越来越

{
"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"
    }
]

} }

If you need to filter out associated entities, you can use @Where . 如果需要过滤掉关联的实体,可以使用@Where It's not a part of JPA specification, but JPA implementation by Hibernate provides this annotation. 它不是JPA规范的一部分,但是Hibernate的JPA实现提供了此注释。 So you can specify any additional conditions: 因此,您可以指定任何其他条件:

public class Company 
{
    ...

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

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

Then you have to change the repository's method name, since you don't need extra condition anymore: 然后,您必须更改存储库的方法名称,因为您不再需要额外的条件:

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

Using this approach you always get companies with only branches that don't have parent. 使用这种方法,您总会得到只有分支机构且没有母公司的公司。 If you need to get all branches of a company (whether they have parent or not) you can create and use repository for Branch entity class. 如果需要获取公司的所有分支机构(无论是否具有母公司),都可以为Branch实体类创建和使用存储库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将嵌套的 JSON object 转换为阵列 Spring 引导? - How do i turn a nested JSON object to an array Spring boot? 如何获取Spring Boot以将Java System.getenv()变量加载到Environment对象中? - How can I get Spring Boot to load the Java System.getenv() variables into the Environment object? 如何使用 Spring 引导从 POST 方法返回 object? - How can I return an object from a POST method with Spring Boot? 如何使用嵌套循环在 spring 引导中导航未知的嵌套 object - How to use nested loop to navigate unknown nested object in spring boot 如何在 java spring 引导中获取价值不是 object - how to get value not object in java spring boot 在 RESTful Spring 引导应用程序中,如何对嵌套的 object 属性进行 url 查询? - In a RESTful Spring Boot application, how do I do url queries of nested object properties? 如何在Spring数据中获取大小嵌套的对象 - How get sized nested object in Spring data 如果http请求的内容类型为urlencoded,如何让我的spring启动控制器读取我对某个对象的http请求的主体? - How can I get my spring boot controller to read the body of my http request to an object if the http request has content type urlencoded? 无法在 Spring Boot 中反序列化嵌套对象“Role” - Unable to deserialize nested object "Role" in Spring boot Spring Boot /百里香嵌套循环对象访问 - spring boot / thymeleaf nested loop object access
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM