views:

732

answers:

2

When trying to save an ID from my parent class into a child class, I keep getting the 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: 

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

non sequitor
While certainly a valid suggestion, that doesn't exactly answer OP's question.
ChssPly76
Absolutely correct ChssPly76 I assumed that his use of the extensions here were unfounded seeing that the `parent_id` is not being propagated correctly. His annotations all look correct beside the changes I suggested. What do you think is going on given the lack of info from the OP?
non sequitor
It's impossible to say without more information.
ChssPly76
A: 

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

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