I have a join table schema relationship represented by OneToMany and ManyToOne associations and am having some trouble getting Hibernate to respect the not-null constraints on the foreign keys.
The schema is similar to the following: TableA PKA <columns A>
Table AB (join table -- two columns as PK) <PKAB1> (FK to Table A) <-- not-null <PKAB2> (FK to Table B) <-- not-null <columns AB>
TableB PKB <columns B>
I am using Hibernate Annotations to represent a portion of this as shown below with object A being the owning side of this bi-directional association.
In Object A @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN) @JoinColumn(name = "PKAB1", nullable = false) @IndexColumn(name = "position") List<AB> getABs() { return mTheList; }
void setABs(List<AB> pTheList) { ValidateNoNullElements(pTheList); <-- this is how I know the not-null constraint is not working. mTheList = pTheList; }
In Object AB @ManyToOne @JoinColumn(name = "PKA", nullable = false, insertable = false, updatable = false)
I read in another post from someone modifying .hbm.xml files directly that the not-null constraint needs to go on the key xml element. I confirmed through hbm2hbmxml that the not-null constraint was not going on the key xml element; though, I have not found a solution through Annotations on how to put it there. The not-null constraint is constantly being placed on the column xml element instead of the key.
Any help would be greatly appreciated!
|