简体   繁体   English

更新参考实体后有什么方法可以更新其他实体中的列?

[英]Is there any way to update columns in other entities after updating reference entity?

This is Project Entity which is used as reference by other entities in my project:这是项目实体,它被我项目中的其他实体用作参考:

@Entity
@Data
@RequiredArgsConstructor
@AllArgsConstructor
public class Project {

    @Id 
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;

    @NonNull private String projectName;
    @NonNull private String projectCode;
    @NonNull private String description;

    @NonNull private Integer resourceSize;

    //other fields

}

Another two Entities Roster and OutsourceRequestForm which copies fields projectName and resourceSize from Project Entity upon creation.另外两个实体RosterOutsourceRequestForm在创建时从项目实体复制字段projectNameresourceSize


@Entity
@Data
@RequiredArgsConstructor
@AllArgsConstructor
public class Roster {

    @Id 
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;

    @Version
    private Long version;

    @NonNull private String projectName;
    @NonNull private Integer resourceSize;

    //other fields

}
@Entity
@RequiredArgsConstructor
@Data
public class OutsourceRequestForm {

    @Id 
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;

    @Version
    private Long version;

    @NonNull private String projectName;
    @NonNull private Integer resourceSize;

    //other fields

}

Change to field resourceSize needs to be reflected in above two entities as well.对字段resourceSize的更改也需要反映在上述两个实体中。 Here is logic for that in service class updating/inserting project entities.这是服务 class 更新/插入项目实体的逻辑。

@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired ProjectRepository projectRepo;

    @Autowired ProjectWebClientService projectWebClientService; 

    @Autowired RosterService rosterService;
    @Autowired OutsourceRequestFormService orfService;

    /**
     * 
     */
    public void updateProjectData() {
        //delete existing records
        projectRepo.deleteAll();
        //fetch data from other server
        List<Project> projectList = projectWebClientService.getProjectData(); 
        //save project data
        projectRepo.save(projectList);

        // ---->   need solution for below part   <----
        projectList.forEach(project-> {
            //find rosters by projectName and update resourceSize
            rosterService.updateRoster(project.getProjectName() ,   project.getResourceSize());
            //find orf by projectName and update resourceSize
            orfService.updateOrf(project.getProjectName() ,   project.getResourceSize());
        });
    }

}

Is there any way i can remove this part projectList.forEach {} from this class and do it at some other location in a better way as It is violating single responsibility principle ?有什么办法可以从这个 class 中删除这部分projectList.forEach {}并以更好的方式在其他位置进行,因为它违反了 单一责任原则

It will also maintain Single Responsibility Principle.它还将维护单一责任原则。 and You can omit the loop as well.并且您也可以省略循环。 But there is an issue in this every time you update project, it will work (if this is requirement then it is fine).但是每次更新项目时都会出现一个问题,它会起作用(如果这是必需的,那很好)。

Please try this:-请试试这个: -

@Entity
@Data
@RequiredArgsConstructor
@AllArgsConstructor
**@EntityListeners({RosterListener.class ,OrfListener.class})**
public class Project {

    @Id 
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;

    @NonNull private String projectName;
    @NonNull private String projectCode;
    @NonNull private String description;

    @NonNull private Integer resourceSize;

    //other fields

}

Listener:-听众:-

import javax.persistence.PostUpdate;
    public class RosterListener{

        @PostUpdate
         void onProjectUpdate(Project roject) {
            RosterService  rosterService = (RosterService ) BeanUtility.getBean("RosterService "); // find this as from your application context environment
            osterService.updateRoster(project.getProjectName(), project.getResourceSize());
         }

    }

import javax.persistence.PostUpdate;
    public class OrfListener{

        @PostUpdate
         void onProjectUpdate(Project roject) {
            OutsourceRequestFormService   outsourceRequestFormService  = (OutsourceRequestFormService  ) BeanUtility.getBean("outsourceRequestFormService  "); // find this as from your application context environment
            outsourceRequestFormService.updateOrf(project.getProjectName(), project.getResourceSize());
         }

    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM