简体   繁体   English

使用级联保存父实体时如何获取子实体 ID

[英]How can I get child entity id when save a parent entity with cascade

I'm using spring-data-jpa.我正在使用 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.我想得到孩子的 id,但我发现我得到的是 null。

I added @GeneratedValue(strategy = GenerationType.IDENTITY) to getId() method, but it didn't work.我将 @GeneratedValue(strategy = GenerationType.IDENTITY) 添加到 getId() 方法,但它不起作用。

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父实体已经在数据库中,所以我直接找到它,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?孩子被保存到数据库并有id,但内存中实体的id仍然为空,我如何在保存父母后获得孩子的id? 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.因为我创建的孩子被其他对象引用,我需要在这个孩子中获取 id 而不是从数据库中找到一个新对象。

You have to work with the returned value from the save method:您必须使用 save 方法的返回值:

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

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.根据代码,您已经创建了child对象并且没有为其元素设置任何值,然后尝试从新创建的对象( child.getId() )中获取元素,除非您从 DB 为其分配值,否则它将始终为空。

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在第 5 行中,我们为其分配了 dB 值

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM