英文:
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="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 and setters....
}
@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 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());
}
{
"kanban_col_id":"ff80808174f95b8c0174f95ba1e30000",
"tasks":[{
"workId":"4028ab37735ae04e01735b4833ba0000"
},
{
"workId":"4028ab37735ae04e01735b4833ba0001"
},
]
}
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);
}
然后你的createTask
和copyTask
方法需要更改为:
@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("select kc from kanban_columns kc left join fetch kc.tasks where kc.id = :id")
kanban_columns findByIdWithTasks(@Param("id") String id);
Define in the kanban_columns
a helper method:
public void addTasks(Collection<Task> tasks) {
tasks.forEach(t -> 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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论