开发者

One-to-Many Unidirectional Parent-Child ID Cascade Save

When trying to save an ID from my parent class into a child class, I keep getting 开发者_如何学JAVAthe error "ERROR - Field 'parent_id' doesn't have a default value"

I have tried all types of mappings. I am using annotations.

Any help on this would be 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;

Child:

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

Thanks.


A late addition in case anyone ever runs into the same issue.

This entity here, when persisted using Hibernate 4.1.8, will cascade the FieldChangeentities, but will not fill the join column:

@Entity
public class Event {

    //ID and other fields here

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "event_id")
    private List<FieldChange<?>> fields = new ArrayList<FieldChange<?>>();

 }

Neither does the insert statement set the event_id column, nor does it update the inserted entity after the fact - the event_id remains null and the relation is lost.

If, however, the @JoinColumn definition is changed like this:

@JoinColumn(name = "event_id", nullable = false)

, then the insert statement includes the event_id column like it should, and all is well.

This may only be a regression in this particular version of Hibernate, but maybe it helps someone.


In your case JPA provider to persist child object with its parent perform at least three queries on db. First two persist the objects by its own. The last one update child object with the foreign key referencing parent. The second query fail because you have a NOT NULL constraint on the foreign key column. You have three options:

  1. Remove NOT NULL constraint on foreign key in the child entity
  2. Use bidirectional relationship
  3. Change JPA provider to one which supports such cases.


You must have something wrong somewhere else because those mappings will work the way they are. They could be better, but they'll work. Specifically, all the @Column annotations are redundant and unnecessary, and as non sequitor noted, you should use the cascade property of JPA's @OneToMany instead of Hibernate's @Cascade. I've created a runnable example with the cleaned-up version of what you posted. If you have git and maven, you can run it with:

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 with two children, saves them, and then loads them and prints out 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'}]}


Change your @OneToMany to @OneToMany(cascade=CascadeType.ALL) use JPA rather than the Hibernate extensions


My guess is that the @JoinColumn annotation needs a referencedColumnName assigned.

@JoinColumn(name = "parent_id", referencedColumnName = "id")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜