如何将许多具有额外实体方法的人中的合并数据获得许多人

发布于 2025-01-17 15:56:31 字数 7116 浏览 1 评论 0原文

以下是我创建的实体,用于通过额外的实体概念实现多对多。

公司:

@Getter
@Setter
@Entity
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "companyId")
public class Company {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long companyId;
    private String companyName;
    
    @OneToMany(mappedBy = "company", fetch = FetchType.EAGER)
    Set<CompanyUserMapping> companyUsers;
}

用户:

@Getter
@Setter
@Entity
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "userId")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long userId;
    private String email;
    private String originalCompanyName;
    
    @OneToMany(mappedBy = "user", fetch = FetchType.EAGER)
    Set<CompanyUserMapping> companyUserMapping;
}

CompanyUserMapping

@Getter
@Setter
@Entity
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class CompanyUserMapping {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "company_id")
    private Company company;

    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "user_id")
    private User user;
    private boolean isExternal;
    private boolean isActive;
}

下面是我向用户添加公司的方式。

{
    "company": {
        "companyName": "XYZ company"
    },
    "user": {
        "email": "[email protected]",
        "originalCompanyName": "XYZ company"
    },
    "active": true,
    "external": false
}

还尝试使用以下代码片段将用户添加到现有公司。

Optional<User>  optionalUser=userRepository.findByEmail(user.getEmail());
Optional<Company> optionalCompany = companyRepository.findById(companyId);
if(optionalUser.isPresent()) {
    User existingUser=optionalUser.get();
    Optional<CompanyUserMapping> optionalCompanyUserMapping=companyUserMappingRepository.findByCompanyAndUser(optionalCompany.get(),existingUser);
    if(optionalCompanyUserMapping.isPresent()) {
        throw new UserExistsForCompanyException("User exists for this company "+companyId);
    }
    CompanyUserMapping companyUserMapping =new CompanyUserMapping();
    companyUserMapping.setCompany(optionalCompany.get());
    companyUserMapping.setUser(existingUser);
    companyUserMapping.setActive(true);
    companyUserMapping.setExternal(false);      
    CompanyUserMapping savedcompanyUserMapping = companyUserMappingRepository.save(companyUserMapping);
    return new ResponseEntity<CompanyUserMapping>(savedcompanyUserMapping, HttpStatus.CREATED);
}
CompanyUserMapping companyUserMapping =new CompanyUserMapping();
companyUserMapping.setCompany(optionalCompany.get());
companyUserMapping.setUser(user);
companyUserMapping.setActive(true);
companyUserMapping.setExternal(false);      
CompanyUserMapping savedcompanyUserMapping = companyUserMappingRepository.save(companyUserMapping);

在这里,如果我尝试通过 Id 获取公司详细信息,则会出现重复信息以及与少数常见用户关联的不需要的公司信息。输出 Json 如下所示

{
    "companyId": 1,
    "companyName": "ABC company",
    "companyUsers": [
        {
            "companyUserMappingId": 1,
            "company": 1,
            "user": {
                "userId": 1,
                "email": "[email protected]",
                "companyUserMapping": [
                    1
                ]
            },
            "active": true,
            "external": false
        },
        {
            "companyUserMappingId": 3,
            "company": 1,
            "user": {
                "userId": 3,
                "email": "[email protected]",
                "companyUserMapping": [
                    3,
                    {
                        "companyUserMappingId": 4,
                        "company": {
                            "companyId": 2,
                            "companyName": "XYZ company",
                            "companyUsers": [
                                {
                                    "companyUserMappingId": 2,
                                    "company": 2,
                                    "user": {
                                        "userId": 2,
                                        "email": "[email protected]",
                                        "companyUserMapping": [
                                            2
                                        ]
                                    },
                                    "active": true,
                                    "external": false
                                },
                                4
                            ]
                        },
                        "user": 3,
                        "active": true,
                        "external": false
                    }
                ]
            },
            "active": true,
            "external": false
        }
    ]
}

如何获取如下所示的合并数据。

{
  "companyId": 1,
  "companyName": "ABC company",
  "companyUsers": [
    {
      "companyUserMappingId": 1,
      "user": {
        "userId": 1,
        "email": "[email protected]"
      },
      "active": true,
      "external": false
    },
    {
      "companyUserMappingId": 3,
      "user": {
        "userId": 3,
        "email": "[email protected]"
      },
      "active": true,
      "external": false
    }
  ]
}

然后,如果我通过 UserId 调用 get User,合并的数据应如下所示。

{
  "userId": 1,
  "email": "[email protected]",
  "associatedCompany": [
    {
      "companyId": 1,
      "companyName": "ABC company"
    }
  ]
}

Below are the Entities I have created to achieve Many to Many with an extra entity concept.

Company :

@Getter
@Setter
@Entity
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "companyId")
public class Company {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long companyId;
    private String companyName;
    
    @OneToMany(mappedBy = "company", fetch = FetchType.EAGER)
    Set<CompanyUserMapping> companyUsers;
}

User :

@Getter
@Setter
@Entity
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "userId")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long userId;
    private String email;
    private String originalCompanyName;
    
    @OneToMany(mappedBy = "user", fetch = FetchType.EAGER)
    Set<CompanyUserMapping> companyUserMapping;
}

CompanyUserMapping

@Getter
@Setter
@Entity
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class CompanyUserMapping {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "company_id")
    private Company company;

    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "user_id")
    private User user;
    private boolean isExternal;
    private boolean isActive;
}

Below is the way I am adding company with the user.

{
    "company": {
        "companyName": "XYZ company"
    },
    "user": {
        "email": "[email protected]",
        "originalCompanyName": "XYZ company"
    },
    "active": true,
    "external": false
}

And also trying to add the user to the existing company using below snippet.

Optional<User>  optionalUser=userRepository.findByEmail(user.getEmail());
Optional<Company> optionalCompany = companyRepository.findById(companyId);
if(optionalUser.isPresent()) {
    User existingUser=optionalUser.get();
    Optional<CompanyUserMapping> optionalCompanyUserMapping=companyUserMappingRepository.findByCompanyAndUser(optionalCompany.get(),existingUser);
    if(optionalCompanyUserMapping.isPresent()) {
        throw new UserExistsForCompanyException("User exists for this company "+companyId);
    }
    CompanyUserMapping companyUserMapping =new CompanyUserMapping();
    companyUserMapping.setCompany(optionalCompany.get());
    companyUserMapping.setUser(existingUser);
    companyUserMapping.setActive(true);
    companyUserMapping.setExternal(false);      
    CompanyUserMapping savedcompanyUserMapping = companyUserMappingRepository.save(companyUserMapping);
    return new ResponseEntity<CompanyUserMapping>(savedcompanyUserMapping, HttpStatus.CREATED);
}
CompanyUserMapping companyUserMapping =new CompanyUserMapping();
companyUserMapping.setCompany(optionalCompany.get());
companyUserMapping.setUser(user);
companyUserMapping.setActive(true);
companyUserMapping.setExternal(false);      
CompanyUserMapping savedcompanyUserMapping = companyUserMappingRepository.save(companyUserMapping);

Here If I try to get Company details by Id, there is repeated information and also unwanted company information which is associated to few common users.The output Json is as below

{
    "companyId": 1,
    "companyName": "ABC company",
    "companyUsers": [
        {
            "companyUserMappingId": 1,
            "company": 1,
            "user": {
                "userId": 1,
                "email": "[email protected]",
                "companyUserMapping": [
                    1
                ]
            },
            "active": true,
            "external": false
        },
        {
            "companyUserMappingId": 3,
            "company": 1,
            "user": {
                "userId": 3,
                "email": "[email protected]",
                "companyUserMapping": [
                    3,
                    {
                        "companyUserMappingId": 4,
                        "company": {
                            "companyId": 2,
                            "companyName": "XYZ company",
                            "companyUsers": [
                                {
                                    "companyUserMappingId": 2,
                                    "company": 2,
                                    "user": {
                                        "userId": 2,
                                        "email": "[email protected]",
                                        "companyUserMapping": [
                                            2
                                        ]
                                    },
                                    "active": true,
                                    "external": false
                                },
                                4
                            ]
                        },
                        "user": 3,
                        "active": true,
                        "external": false
                    }
                ]
            },
            "active": true,
            "external": false
        }
    ]
}

How do I get the consolidated data like below.

{
  "companyId": 1,
  "companyName": "ABC company",
  "companyUsers": [
    {
      "companyUserMappingId": 1,
      "user": {
        "userId": 1,
        "email": "[email protected]"
      },
      "active": true,
      "external": false
    },
    {
      "companyUserMappingId": 3,
      "user": {
        "userId": 3,
        "email": "[email protected]"
      },
      "active": true,
      "external": false
    }
  ]
}

And then if I call get User by UserId, the consolidated data should be like below.

{
  "userId": 1,
  "email": "[email protected]",
  "associatedCompany": [
    {
      "companyId": 1,
      "companyName": "ABC company"
    }
  ]
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文