Home > Back-end >  MapStruct how to combine the Jpa annotations to update the object?
MapStruct how to combine the Jpa annotations to update the object?

Time:12-04

I have two entity class: departments and users, departments and users is a one-to-many relationship, the User for a party, for a party, so a User with @ ManyToOne, added the @onetomany, then I would like to update to the customer, update the content is to modify the User's Department, the User is the User entity classes, the Department is the Department entity class, UserUpdateReq is to modify the User information data classes, UserConvert is transformation class. Now encountered a problem is if the @ ManyToOne or added on the @onetomany CasedBy will lead to update the User's Department ID at the same time, the Department of other fields to update is null, don't add CasedBy and can lead to users and departments can't connected. Do you have any solution?

 
@ Data
@ NoArgsConstructor
@ AllArgsConstructor
@ the Entity
@ Table (name="t_user")
Public class User extends BaseEntity {

@ the Column (columnDefinition="varchar (20) DEFAULT 'COMMENT' user name '", nullable=false)
Private String username;
@ the Column (columnDefinition="varchar (100) the DEFAULT 'COMMENT' password '", nullable=false)
private String password;

@ ManyToOne (cascade=CascadeType. ALL the fetch=FetchType. LAZY)
@ JoinColumn (name="dept_id")
Private Department dept;

@ ManyToOne (cascade=CascadeType. ALL the fetch=FetchType. LAZY)
@ JoinColumn (name="role_id")
Private Role Role;

}

 
@ Data
@ NoArgsConstructor
@ AllArgsConstructor
@ the Entity
@ Table (name="t_dept")
Public class Department extends BaseEntity {

@ the Column (columnDefinition="varchar (20) DEFAULT 'COMMENT' department name")
Private String the deptName.
@ the Column (columnDefinition="varchar (20) DEFAULT 'COMMENT' names")
Private String dept;
@ the Column (columnDefinition="int the DEFAULT 0 COMMENT 'superior department ID")
Private Integer parentDeptId;

The @onetomany (mappedBy="dept", cascade=CascadeType. ALL, fetch=FetchType. LAZY)
Private Set
The @onetomany (mappedBy="dept", cascade=CascadeType. ALL, fetch=FetchType. LAZY)
Private Set}

 
@ Data
@ NoArgsConstructor
@ AllArgsConstructor
Public class UserUpdateReq {

Private Integer userId.
Private String username;
Private Integer deptId;
}

 
@ Mapper (componentModel="spring")
Public interface UserConvert {


The User userUpdateReqToUser (userUpdateRequser the req);

}

CodePudding user response:

Feel the MapStrut nothing much, advice or a mapper. XML is flexible
  • Related