gpt4 book ai didi

java - Hibernate jpa oneToMany : how to add to "one" list of many children

转载 作者:行者123 更新时间:2023-12-01 11:16:27 25 4
gpt4 key购买 nike

你能帮我解决一些问题吗?我有两个实体项目和页面通过“一对多”连接:

项目模型(也与用户模型相关)

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonProperty
private Integer id;

@ManyToOne(optional = false, fetch = FetchType.EAGER)
@JoinColumn(name = "username", referencedColumnName = "username", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private User user;

@Column(name="projectName", unique = true, nullable = false)
@JsonProperty
private String projectName;

@Column(name="style")
@JsonProperty
private String style;

@Column(name="menu")
@JsonProperty
private String menu;

@JsonProperty
@Fetch(value = FetchMode.SELECT)
@OneToMany(orphanRemoval=true, mappedBy = "project", cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
private Set<Page> pages;
//getters and setters

页面模型:

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonProperty
private Integer id;

@Column(name="content")
@JsonProperty
private String content;

@Column(name="pageName", nullable = false)
@JsonProperty
private String pageName;

@ManyToOne(optional = false)
@JoinColumn(name = "project", referencedColumnName = "projectName", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private Project project;
//getters and setters

现在我想做这样的事情:

@Override
public void updatePages(Set<Page> pages, Integer projectId) {
Project project = entityManager.find(Project.class, projectId);
project.setPages(pages);
}

但是 Hibernate 查询没有结果和错误。

Hibernate: 
select
project0_.id as id1_1_0_,
project0_.menu as menu2_1_0_,
project0_.projectName as projectN3_1_0_,
project0_.style as style4_1_0_,
project0_.username as username5_1_0_,
user1_.id as id1_4_1_,
user1_.email as email2_4_1_,
user1_.enabled as enabled3_4_1_,
user1_.password as password4_4_1_,
user1_.username as username5_4_1_,
userroles2_.username as username3_4_2_,
userroles2_.id as id1_2_2_,
userroles2_.id as id1_2_3_,
userroles2_.role as role2_2_3_,
userroles2_.username as username3_2_3_
from
projects project0_
inner join
users user1_
on project0_.username=user1_.username
left outer join
roles userroles2_
on user1_.username=userroles2_.username
where
project0_.id=?
Hibernate:
/* load project.model.User */ select
user0_.id as id1_4_2_,
user0_.email as email2_4_2_,
user0_.enabled as enabled3_4_2_,
user0_.password as password4_4_2_,
user0_.username as username5_4_2_,
projects1_.username as username5_4_4_,
projects1_.id as id1_1_4_,
projects1_.id as id1_1_0_,
projects1_.menu as menu2_1_0_,
projects1_.projectName as projectN3_1_0_,
projects1_.style as style4_1_0_,
projects1_.username as username5_1_0_,
userroles2_.username as username3_4_5_,
userroles2_.id as id1_2_5_,
userroles2_.id as id1_2_1_,
userroles2_.role as role2_2_1_,
userroles2_.username as username3_2_1_
from
users user0_
left outer join
projects projects1_
on user0_.username=projects1_.username
left outer join
roles userroles2_
on user0_.username=userroles2_.username
where
user0_.username=?
Hibernate:
select
pages0_.project as project4_1_0_,
pages0_.id as id1_0_0_,
pages0_.id as id1_0_1_,
pages0_.content as content2_0_1_,
pages0_.pageName as pageName3_0_1_,
pages0_.project as project4_0_1_
from
pages pages0_
where
pages0_.project=?

我想更新项目中的页面。最好的方法是什么?

UPD

Project project = entityManager.find(Project.class, projectId);
entityManager.createQuery("DELETE FROM Page p WHERE p.project = :project")
.setParameter("project",project)
.executeUpdate();

最佳答案

关联 Project-Pages 的所有者端位于 Page 实体上,在 hibernate 状态下(因为它解析双向一对多的方式),这意味着您必须设置 Page.project 对于您想要与 Project 链接的每个 Page。如果您只将页面添加到项目的页面集合中,它将不起作用。

这样做,

@Override
public void updatePages(Set<Page> pages, Integer projectId) {
Project project = entityManager.find(Project.class, projectId);
for (Page p: pages) {
p.setProject (project);
//the pages are new? then also do em.persist(p)
}
}

根据您的实体模型,您现在看到的 select 语句需要检索项目并在调用 entityManager.find 方法时生成。

关于java - Hibernate jpa oneToMany : how to add to "one" list of many children,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31773936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com