Hibernate jpa oneToMany:如何添加到" one"许多孩子的名单

时间:2015-08-02 16:22:18

标签: java hibernate jpa

你能解决一些问题吗?我有两个实体项目和页面连接,一个到多个':

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

@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查询没有rezult和错误。

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

1 个答案:

答案 0 :(得分:1)

Project-Pages关联的所有者方位于Page实体上,处于休眠状态(因为它解析双向一对多的方式),这意味着您必须设置Page.project对于要与Page关联的每个Project。如果您只将Pages添加到Project的页面集合中,它将无法工作。

这样做,

@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方法时生成的。