[英]Hibernate :saving child entity
i have 2 tables: 我有2张桌子:
Parent table: phasesetups 父表: phasesetups
| id | created | modified | name | model | model_id | uid | description | old_model_id |
+-----+---------------------+---------------------+-------------------------------------------------+-------+----------+------+------------------+--------------+
| 556 | 2016-08-26 14:08:07 | 2016-08-26 14:08:07 | EQ: 1 | PREP: 2 | ANALYSIS: 3 | BIOINF: 4 | Order | 0 | uid | first phasesetup | ng_2004741 |
Child table: phases 子表: 阶段
+------+---------------------+---------------------+------+-----------+---------------+--------------+
| id | created | modified | name | duration | phasesetup_id | phasetype_id |
+------+---------------------+---------------------+------+-----------+---------------+--------------+
| 2263 | 2016-08-26 14:08:07 | 2016-08-26 14:08:07 | NULL | 345600000 | NULL | 4 |
| 2266 | 2016-08-26 14:08:07 | 2016-08-26 14:08:07 | NULL | 86400000 | NULL | 1 |
| 2269 | 2016-08-26 14:08:07 | 2016-08-26 14:08:07 | NULL | 172800000 | NULL | 2 |
| 2272 | 2016-08-26 14:08:07 | 2016-08-26 14:08:07 | NULL | 259200000 | NULL | 3 |
+------+---------------------+---------------------+------+-----------+---------------+--------------+
My entites: 1.PhaseSetup 我的实体:1.PhaseSetup
@Entity
@Table(name = "phasesetups")
public class PhaseSetup extends AbstractEntity {
@Column(name = "model_id")
private Long modelId;
@Column(nullable = false)
private String model;
@Column(name = "old_model_id")
private String oldModelId;
@Column(nullable = false)
private String uid;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "phasesetup_id", cascade = CascadeType.PERSIST)
private Set<Phase> phases;
2nd Phases: 第二阶段:
@Entity
@Table(name = "phases")
public class Phase extends AbstractEntity {
@ManyToOne
@JoinColumn(name = "phasetype_id")
private Phasetype phasetype;
private Long phasesetup_id;
private Long duration;
Problem: i want to save phases along with phasesetups and i am able to save them but phasesetup_id is still null. 问题:我想将阶段和相位设置一起保存,并且能够保存它们,但是phasesetup_id仍然为空。
Is not phasesetup_id supposed to be saved automatically ?as it is mappedby phasesetup_id ? 由于phasephaseup_id映射了phasesetup_id,因此不应该将其自动保存吗?
Please help me what am i missing here ? 请帮助我,我在这里想念什么? Thanks. 谢谢。
You donot require private Long phasesetup_id;
您不需要private Long phasesetup_id;
in Phase
. 在Phase
。 Change PhaseSetup as follows 如下更改PhaseSetup
@OneToMany(fetch = FetchType.EAGER, mappedBy = "phasesetup_id", cascade = CascadeType.PERSIST)
private Set<Phase> phases;
to 至
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
@JoinColumn(name = "phasesetup_id")
private Set<Phase> phases;
You do not require mappedBy in unidirectional JPA relationships.Read here 您不需要单向JPA关系中的mappingBy。阅读此处
There is need to define relationship between PhaseSetups and Phase in Phase Entity. 需要定义“相位设置”和“相位实体”中的“相位”之间的关系。 So define phasesetup_id in Phase as object like 因此,将Phase中的phasesetup_id定义为像
@ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = " phasesetup_id
", referencedColumnName = " uid
", insertable = true, updatable = true) private PhaseSetup phasesetup; @ManyToOne(fetch = FetchType.EAGER)@JoinColumn(name =“ phasesetup_id
”,referencedColumnName =“ uid
”,insertable = true,可更新= true)私有PhaseSetup phasesetup;
I think, It will resolve your problem. 我认为,它将解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.