更新引用实体后,有没有办法更新其他实体中的列?

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

这是项目实体,在我的项目中被其他实体用作参考:

@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

}

另外两个实体 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

}

字段 resourceSize 的更改也需要反映在上述两个实体中。 这是服务 class updating/inserting 项目实体中的逻辑。

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

}

有什么方法可以从这个 class 中删除这部分 projectList.forEach {} 并在其他位置以更好的方式进行,因为它违反了 single responsibility principle 吗?

它也将保持单一职责原则。你也可以省略循环。但是每次更新项目时都会有一个问题,它会起作用(如果这是要求那么没关系)。

请试试这个:-

@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

}

听众:-

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

    }