4

I'm using spring-data-jpa. After adding a child to parent entity, i save the parent to database. I'd like to get the child's id, but I found what I get is null.

I added @GeneratedValue(strategy = GenerationType.IDENTITY) to getId() method, but it didn't work.

here is model:

@Entity
public class Parent {

    private Integer id;
    private List<Child> childList;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer getId() {
        return id;
    }

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "parent_id")
    public List<Child> getChildList() {
        return childList;
    }

    // setters.....

}

@Entity
public class Child {

    private Integer id;
    private String name;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer getId() {
        return id;
    }

    @Cloumn("name")
    public String getName() {
        return name;
    }

}

the parent entity is already in database, so i find it directly, ParentRepository entends JpaReportory
here is my test code:

Parent parent = parentRepository.findById(1);
Child child = new Child();
child.setName("child");
parent.getChildList().add(child);
parentRepository.save(parent);

System.out.println("child's id: " + child.getId());

the output i get is:

child's id: null

the child is saved to database and has id, but the id of entity in memory is still null, how can I get child's id after saving parent? And because the child I create was cited by other object, i need to get id just in this child rather than find a new object from database.

2 Answers2

3

You have to work with the returned value from the save method:

Parent parent = parentRepository.findById(1);
Child child = new Child();
parent.getChildList().add(child);
parent = parentRepository.save(parent); <---------- use returned value with ids set

System.out.println("child's id: " + parent.getChildList().get(0).getId()); <-- access saved child through parent list
C. Weber
  • 907
  • 5
  • 18
  • returned parent is a new objcet, so is the child, but the original child was cited by other object, i need to update id for the original child, how can i do? – Zhenhua Sheng Jul 16 '19 at 12:09
  • But this child hasn´t been saved before, right? It is a new database entry? – C. Weber Jul 16 '19 at 13:44
  • For further reading why you must use the returned value https://stackoverflow.com/q/8625150/7634201 – C. Weber Jul 16 '19 at 14:38
  • @C.Weber the below line will print **child object value**, not **ID value** of child object `System.out.println("child's id: " + parent.getChildList().get(0));` please update code accordingly – TheSprinter Jul 17 '19 at 07:00
  • @TheSprinter thanks for the hint. Added a getID() to the code. – C. Weber Jul 17 '19 at 07:04
0

As per the code, you have created child Object and not set any value to its element and then trying to get element from newly created object (child.getId()) It will alwas be null, unless you assign value from DB to it.

Parent parent = parentRepository.findById(1);
Child child = new Child(); // Empty child object created
parent.getChildList().add(child);
parentRepository.save(parent);

System.out.println("child's id: " + child.getId()); //Referring empty child object

Here what you can do is :
in the 5th line we have assinged dB value to it

Parent parent = parentRepository.findById(1);
Child child = new Child(); // Empty child object created
parent.getChildList().add(child);
parent = parentRepository.save(parent);
child = parent.getChildList().get(0);// assing db value to it( assingning 1st value of `ChildList`)
System.out.println("child's id: " + child.getId()); //now Referring non-empty child object
TheSprinter
  • 1,523
  • 17
  • 30