如何将许多具有额外实体方法的人中的合并数据获得许多人
以下是我创建的实体,用于通过额外的实体概念实现多对多。
公司:
@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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论