Java – one to many one-way parent-child ID cascade saving

When I try to save the ID from the parent class to the child class, I keep getting errors

I tried all types of mappings I'm using notes

Any help in this regard will be greatly appreciated

parent:

@Id
      @Column(name="id")
      @GeneratedValue(strategy=GenerationType.AUTO)
      private long id;
      @Column(name="description")
      private String description;
      @OneToMany
      @Cascade(value= {org.hibernate.annotations.CascadeType.SAVE_UPDATE,org.hibernate.annotations.CascadeType.DELETE})
      @JoinColumn(name="parent_id")
      private List<Child> children;

Children:

@Id
  @Column(name="id")
  @GeneratedValue(strategy=GenerationType.AUTO)
  private long id;
  @Column(name="description")
  private String description;

thank you.

Solution

You have to make mistakes elsewhere because these mappings will run their way They may be better, but they will work Specifically, all @ column annotations are redundant and unnecessary, and if you don't notice, you should use the cascading attribute of @ onetomany of JPA instead of @ cascade. Of hibernate I have created a runnable example with the cleanup version you released If you have GIT and maven, you can run it:

git clone git://github.com/zzantozz/testbed tmp
cd tmp
mvn -q compile exec:java \
    -Dexec.mainClass=rds.hibernate.UnidirectionalManyToOneJoinColumn \
    -pl hibernate-unidirectional-one-to-many-with-join-column

It creates a parent node with two child nodes, saves them, loads them and prints the graph The output is:

Creating parent with two children
Loading saved parent
Parent{description='parent',children=[Child{description='child 2'},Child{description='child 1'}]}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>