将子实体添加到现有父实体中,使用 Hibernate 的一对多关系。

huangapple go评论73阅读模式
英文:

add child entity to an exsisting parent hibernate onetomany

问题

我刚接触 Hibernate
我想要向现有的父实体添加子实体

@Entity
@Table(name="kanban_col")
public class kanban_columns {
    @Id
    @GeneratedValue(generator="system-uuid")
    @GenericGenerator(name="system-uuid", strategy = "uuid")
    private String id;

    @Column(name="col_name")
    private String colName;

    @Column(name="kbid")
    private String boardId;

    @OneToMany(mappedBy = "kanban_col_id", cascade = CascadeType.PERSIST, orphanRemoval = true, fetch=FetchType.LAZY)
    private List<Tasks> tasks;

    // Getter 和 Setter....
}

@Entity
@Table(name="kanban_tasks")
public class Tasks {
    @Id
    @GeneratedValue(generator="system-uuid")
    @GenericGenerator(name="system-uuid", strategy = "uuid")
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JsonBackReference
    @JoinColumn(name = "cid", updatable = true, insertable = true, referencedColumnName = "id")
    private kanban_columns kanban_col_id;

    @Column(name="work_id")
    private String workId;

    // Getter 和 Setter....
}

service

@Transactional
public kanban_columns createTask(final kanban_columns source) {
    final kanban_columns destination = new kanban_columns();

    copyTask(source, destination);

    repository.save(destination);

    return destination;
}

@Transactional
public void save(final kanban_columns attachment) {
    repository.save(attachment);
}

public void copyTask(final kanban_columns source, final kanban_columns destination) {
    destination.setTasks(source.getTasks());
}

{
    "kanban_col_id":"ff80808174f95b8c0174f95ba1e30000",
    "tasks":[
        {"workId":"4028ab37735ae04e01735b4833ba0000"},
        {"workId":"4028ab37735ae04e01735b4833ba0001"}
    ]
}
我尝试进行操作,但会在“kanban_col”中创建新行,我需要在现有行中插入
请告诉我如何在@onetomany中为现有父实体添加新行,以及如何更新、删除现有父实体的子行
谢谢..!!
英文:

I am new to hibernate
i want to add child to an exsisting parent entity

@Entity
@Table(name=&quot;kanban_col&quot;)
public class kanban_columns {
    @Id
    @GeneratedValue(generator=&quot;system-uuid&quot;)
    @GenericGenerator(name=&quot;system-uuid&quot;, strategy = &quot;uuid&quot;)
    private String id;

    @Column(name=&quot;col_name&quot;)
    private String colName;

    @Column(name=&quot;kbid&quot;)
    private String boardId;

    @OneToMany(mappedBy = &quot;kanban_col_id&quot;,cascade = CascadeType.PERSIST,orphanRemoval = true,fetch=FetchType.LAZY)
    private List&lt;Tasks&gt; tasks;

    Getter and setters....
}
@Entity
@Table(name=&quot;kanban_tasks&quot;)
public class Tasks {
    @Id
    @GeneratedValue(generator=&quot;system-uuid&quot;)
    @GenericGenerator(name=&quot;system-uuid&quot;, strategy = &quot;uuid&quot;)
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JsonBackReference
    @JoinColumn(name = &quot;cid&quot;, updatable = true, insertable = true,referencedColumnName = &quot;id&quot;)
    private kanban_columns kanban_col_id;

    @Column(name=&quot;work_id&quot;)
    private String workId;

Getter and setters....
}

service

    @Transactional
    public kanban_columns createTask(final kanban_columns source) {
        final kanban_columns destination = new kanban_columns();

        copyTask(source, destination);

        repository.save(destination);

        return destination;
    }

    @Transactional
    public void save(final kanban_columns attachment)
    {
        repository.save(attachment);
    }
 public void copyTask(final kanban_columns source, final kanban_columns destination){
        destination.setTasks(source.getTasks());
    }
{
    &quot;kanban_col_id&quot;:&quot;ff80808174f95b8c0174f95ba1e30000&quot;,
        &quot;tasks&quot;:[{
        &quot;workId&quot;:&quot;4028ab37735ae04e01735b4833ba0000&quot;
    },
{
        &quot;workId&quot;:&quot;4028ab37735ae04e01735b4833ba0001&quot;
    },
]
    
}

I am trying to do but a new row in kanban_col is created but i need to insert into existing one
please tell me how to add new row in child entity for existing parent in @onetomany and also how to update,delete child row for existing parent
thank you..!!

答案1

得分: 1

你在createTask方法中创建了kanban_columns的新实例,因此新行会被添加到数据库中。你应该找到已有的关联任务的kanban_columns,然后将新任务添加到列表中。

你需要在仓库中新建一个方法,加载并返回已经关联任务的kanban_columns实例:

@Query("select kc from kanban_columns kc left join fetch kc.tasks where kc.id = :id")
kanban_columns findByIdWithTasks(@Param("id") String id);

kanban_columns中定义一个辅助方法:

public void addTasks(Collection<Task> tasks) {
    tasks.forEach(t -> t.setKanban_column_id(this));
    this.tasks.addAll(tasks);
}

然后你的createTaskcopyTask方法需要更改为:

@Transactional
public kanban_columns createTask(final kanban_columns source) {
    final kanban_columns destination = repository.findByIdWithTasks(source.getId());

    copyTask(source, destination);

    repository.save(destination);

    return destination;
}

public void copyTask(final kanban_columns source, final kanban_columns destination) {
    destination.addTasks(source.getTasks());
}

要删除任务,你需要将级联操作从PERSIST更改为ALL,并在kanban_columns中定义一个类似的方法:

public void deleteTask(Task task) {
    this.tasks.remove(task);
}
英文:

You create the new instance of kanban_columns in the createTask method, so the new row is added in the database. You should find the existing kanban_columns with the associated tasks and then add a new task to list.

You need a new method in the repository, that loads and returns the kanban_columns instance with the already associated tasks:

@Query(&quot;select kc from kanban_columns kc left join fetch kc.tasks where kc.id = :id&quot;)
kanban_columns findByIdWithTasks(@Param(&quot;id&quot;) String id);

Define in the kanban_columns a helper method:

public void addTasks(Collection&lt;Task&gt; tasks) {
    tasks.forEach(t -&gt; t.setKanban_column_id(this));
    this.tasks.addAll(tasks);
}

And your createTask and copyTask methods should be changed to:

@Transactional
public kanban_columns createTask(final kanban_columns source) {
    final kanban_columns destination = repository.findByIdWithTasks(souce.getId());

    copyTask(source, destination);

    repository.save(destination);

    return destination;
}

public void copyTask(final kanban_columns source, final kanban_columns destination) {
    destination.addTasks(source.getTasks());
}

To delete a task you will need to change cascade from PERSIST to ALL and define a similar method in the kanban_columns:

public void deleteTask(Task task) {
    this.tasks.remove(task);
}

huangapple
  • 本文由 发表于 2020年10月6日 00:26:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/64212581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定