【问题标题】:How can I get child entity id when save a parent entity with cascade使用级联保存父实体时如何获取子实体ID
【发布时间】:2019-11-24 22:28:54
【问题描述】:

我正在使用 spring-data-jpa。将子实体添加到父实体后,我将父实体保存到数据库。我想获取孩子的 id,但我发现我得到的是 null。

我在 getId() 方法中添加了@GeneratedValue(strategy = GenerationType.IDENTITY),但它不起作用。

这是型号:

@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;
    }

}

父实体已经在数据库中,所以我直接找到它,ParentRepository entends JpaReportory
这是我的测试代码:

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

我得到的输出是:

child's id: null

child 保存到数据库并且有id,但是内存中entity 的id 还是null,保存parent 后如何获取child 的id?而且因为我创建的子对象被其他对象引用,所以我需要只在这个子对象中获取 id,而不是从数据库中查找新对象。

【问题讨论】:

    标签: java spring-data-jpa


    【解决方案1】:

    您必须使用 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
    

    【讨论】:

    • 返回的parent是新的objcet,child也是,但是原来的child被其他object引用了,我需要更新原来child的id,怎么办?
    • 但是这孩子以前没得救过吧?这是一个新的数据库条目?
    • 进一步阅读为什么必须使用返回值stackoverflow.com/q/8625150/7634201
    • @C.Weber 下面的行将打印 子对象值,而不是子对象 System.out.println("child's id: " + parent.getChildList().get(0));ID 值 请相应地更新代码跨度>
    • @TheSprinter 感谢您的提示。在代码中添加了一个 getID()。
    【解决方案2】:

    根据代码,您已创建 child 对象,但未为其元素设置任何值,然后尝试从新创建的对象中获取元素 (child.getId()) 除非您从 DB 为它分配值,否则它将始终为 null。

    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
    

    在这里你可以做的是:
    在第 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
    

    【讨论】:

      猜你喜欢
      • 2016-09-17
      • 1970-01-01
      • 1970-01-01
      • 2016-05-17
      • 1970-01-01
      • 2012-11-10
      • 1970-01-01
      • 1970-01-01
      • 2017-09-27
      相关资源
      最近更新 更多