多对多持久性JPA

quhf5bfb  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(125)

我试图持久化实体使用JPA。我有2个多对多关系的实体,我希望能够做双方的所有crud操作。

以下是我的表格示例:

@Entity
@Table(name = "workflow_tasks")
@Getter @Setter
@AllArgsConstructor @NoArgsConstructor
public class WorkflowTaskEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(length = 30)
    private String workflowTaskId;

    @Column(unique = true)
    private String name;

    @ManyToMany(
        cascade = CascadeType.ALL,
        fetch = FetchType.EAGER
    )
    @JoinTable(
        name="workflow_tasks_workflow_tasks_settings",
        joinColumns = @JoinColumn(name = "workflow_task_id", referencedColumnName = "id"),
        inverseJoinColumns = @JoinColumn(name = "setting_id", referencedColumnName = "id")
    )
    private Set<WorkflowTaskSettingEntity> settings;
...
}

字符串

另一边:

@Entity
@Table(name = "workflow_task_settings")
@Getter @Setter
@AllArgsConstructor @NoArgsConstructor
public class WorkflowTaskSettingEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(length = 30)
    private String workflowTaskSettingId;

    @Column(unique = true)
    private String name;

    @ManyToMany(
            cascade = CascadeType.ALL,
            mappedBy = "settings",
            fetch = FetchType.EAGER
    )
    private Set<WorkflowTaskEntity> workflowTasks;
}


下面是一个实现的repository接口:

public interface WorkflowTaskSettingRepository
        extends CrudRepository<WorkflowTaskSettingEntity, Long> {
    Page<WorkflowTaskSettingEntity> findAll(Pageable pageRequest);
    List<WorkflowTaskSettingEntity> findAll();
    WorkflowTaskSettingEntity findByWorkflowTaskSettingId(String workflowTaskSettingId);

    WorkflowTaskSettingEntity findByValue(String name);

    @Modifying
    @Transactional
    @Query(value = "DELETE FROM WorkflowTaskSettingEntity c WHERE c.id = :id")
    void deleteById(@Param("id") Long id);
}

以下是我必须处理的不同情况:

  • 一次性创建WorkflowTaskSettingEntity和WorkflowTaskEntity对象
  • 创建WorkflowTaskSettingEntity并将其链接到多个WorkflowTaskEntity对象
  • 创建WorkflowTaskSettingEntity并将其链接到多个WorkflowTaskEntity
  • 删除两个表之间的关系
  • 删除工作流任务设置实体和关系
  • 删除工作流任务实体和关系
    我创建条目时的第一个问题是我没有持久化所有关系:

我首先创造了:

  • 4个WorkflowTaskEntity条目
  • 我尝试持久化链接到我创建的4个条目的10个WorkflowTaskSettingEntity对象
  • 第一个WorkflowTaskSettingEntity已正确持久化


的数据

**当持久化第二个对象时,它变得很奇怪:**理论上我应该有:

1-1, 1-2, 1-3, 1-4, 2-1, 2-2, 2-3, 2-4 ...


等等...但结果是这样的


**我的第二个问题涉及性能和代码长度:**我记录了我的SQL查询,并注意到对于基本的选择和插入有太多的SQL查询。我也想知道为什么我有分离实体的问题,并且必须持久化一个对象两次..
创建代码如下:

@Override
public WorkflowTaskSettingDto create(WorkflowTaskSettingDto workflowTaskSettingDto) {
    Set<WorkflowTaskDto> workflowTaskDtos = workflowTaskSettingDto.getWorkflowTasks();
    WorkflowTaskSettingEntity workflowTaskSettingEntity = modelMapper.map(
            workflowTaskSettingDto, WorkflowTaskSettingEntity.class);

    workflowTaskSettingEntity.setWorkflowTasks(null);
    workflowTaskSettingEntity = repository.save(workflowTaskSettingEntity);
    Set<WorkflowTaskEntity> workflowTaskEntities = getWorkflowTasks(workflowTaskDtos);
    workflowTaskSettingEntity.addWorkflowTasks(workflowTaskEntities);
    workflowTaskSettingEntity = repository.save(workflowTaskSettingEntity);

    return modelMapper.map(workflowTaskSettingEntity, WorkflowTaskSettingDto.class);
}

private Set<WorkflowTaskEntity> getWorkflowTasks(Set<WorkflowTaskDto> workflowTaskDtos) {
    Set<WorkflowTaskEntity> workflowTaskEntities = new HashSet<>();
    workflowTaskDtos.forEach(workflowTaskDto -> {
        WorkflowTaskEntity workflowTaskEntity =
                appService.workflowTaskService().getById(workflowTaskDto.getWorkflowTaskId());
        if (workflowTaskEntity == null) {
            WorkflowTaskDto create = appService.workflowTaskService().create(workflowTaskDto);
            workflowTaskEntity = appService.workflowTaskService().getById(create.getWorkflowTaskId());
        }
        workflowTaskEntity.setSettings(workflowTaskEntity.getSettings());
        workflowTaskEntities.add(workflowTaskEntity);
    });
    return workflowTaskEntities;
}

ao218c7q

ao218c7q1#

上一次我在java中做了很多-2-许多是大约5年前,因为我避免了这样的事情,但据我所知,你需要定义一个中间表
你叫"workflow_tasks_workflow_tasks_settings"

@Entity
@IdClass(WorkflowTasksSettingsMappingIds.class) // nice to add some id on these two i believe
@Table(name = "workflow_tasks_workflow_tasks_settings")
public class WorkflowTasksWorkflowTasksSettings {

    @NotNull
    @Column(name = "workflow_task_id")
    private String workflowTasksId;

    @NotNull
    @Column(name = "setting_id")
    private String settingId;

  
    @Id
    @ManyToOne
    @JoinColumn(name = "workflow_task_id", insertable = false, updatable = false)
    private WorkflowTaskEntity taask;

    @Id
    @ManyToOne
    @JoinColumn(name = "setting_id", insertable = false, updatable = false)
    private WorkflowTaskSettingEntity setting;

// getters setters

}

字符串
和ID

@Embeddable
public class ArticleOwnerId implements Serializable {

    @Column(name = "workflow_task_id")
    private String workflowTaskId;

    @Column(name = "setting_id")
    private String settingId;

  //getters setters
}


我是这么想

相关问题