lesnar
lesnar

Reputation: 2480

Hibernate :saving child entity

i have 2 tables:

Parent table: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

@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.

Is not phasesetup_id supposed to be saved automatically ?as it is mappedby phasesetup_id ?

Please help me what am i missing here ? Thanks.

Upvotes: 0

Views: 55

Answers (2)

Rohit
Rohit

Reputation: 2152

You donot require private Long phasesetup_id; in Phase. Change PhaseSetup as follows

   @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

Upvotes: 1

Lok Raj
Lok Raj

Reputation: 30

There is need to define relationship between PhaseSetups and Phase in Phase Entity. So define phasesetup_id in Phase as object like

@ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "phasesetup_id", referencedColumnName = "uid", insertable = true, updatable = true) private PhaseSetup phasesetup;

I think, It will resolve your problem.

Upvotes: 0

Related Questions