Sunday, July 18, 2010

Hibernate (2)

We use dbunit to populate the database with some data in testing phase. I tried to generate some new dbunit export with new "order" value. It always fail to load, I have to revert the data back to the time without any order data. It said it could not find the new column for "order". I finally decided to sit down and work on it. After maybe 20 minutes guessing, I figure it is probably the maven-hibernate3 plugin generated the ddl without those column. I delete this plug-in and it goes well.

I think I also found a bug in their documentation. In their example,
(3.5.1Final), the id field is declared non-nullable.

@Entity
public class Parent {
@OneToMany
@OrderColumn(name="order")
@JoinColumn(name="parent_id", nullable=false)
private List<Child> children;
...
}

@Entity
public class Child {
...
@ManyToOne
@JoinColumn(name="parent_id", insertable=false, updatable=false, nullable=false)
private Parent parent;
...
}

I have something very similar, a MirexSubmission with a list of MirexNote's. I also make the cascade from MirexSubmision.


@Entity
@Table(name="mirex_submission")
public class MirexSubmission implements Serializable {
...
@OneToMany(cascade={CascadeType.PERSIST,CascadeType.MERGE},fetch=FetchType.EAGER)
@OrderColumn(name="note_order")
@JoinColumn(name="submission_id")
public List<MirexNote> getNotes() {
return notes;
}

.....
}



@Entity
@Table(name="mirex_note")
public class MirexNote implements Serializable{
...
@ManyToOne
@JoinColumn(name="submission_id",insertable=false, updatable=false,nullable=true)
public MirexSubmission getSubmission() {
return submission;
}
...
}


However, it always give me some error when I try save (Hibernate merge()) about "no default value for "submission_id" column. It does not matter I merge the MirexNote or MirexSubmission object, in which case it cascade to MirexNote. I check the schema, "submission_id" column does not allow null value, but with out the default. And obviously, it is no wonder the insert on this table fail. Finally I change to nullable=true and things worked out.

Correction (Aug. 3rd): It seems to work fine if both ends (many-to-many, one-to-many) set nullable=false.

No comments :