Update.
I fixed this by simply making the mapping a bidirectional mapping and adding the userId to the sheets table. It also required that I remember to set the user on every sheet before save.
For anyone interested, here is the code that is now working:
Code:
@Entity
@Table(name = "User")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "userId")
private Integer userId;
@OneToMany(mappedBy="user")
@Cascade({ org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
private Set<Sheet> sheets;
}
@Entity
@Table(name = "Sheet")
public class Sheet {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "sheetId")
private int sheetId;
@ManyToOne
@JoinColumn(name="userId")
private User user;
}
DB UPDATE:
Removed JoinTable userSheets
Added userId to Sheets (created FK to user)
CODE UPDATE:
When instantiating a new sheet, need to make sure you call setUser() on Sheet to create the relationship.
I hate to be 'that guy' but I don't think the unidirectional with a Join table works, or to be more PC, works as described in the doc(
http://docs.jboss.org/hibernate/stable/ ... ollections). I don't have time to debug through the hibernate src, but I assume there is an undocumented setting/step that is required to establish the relationship and have the JoinTable specified updated on save.
I am still hoping I am just wrong and someone can point out my error, especially considering the dogmatic tone the hibernate docs take : "A unidirectional one to many using a foreign key column in the owned entity is not that common and not really recommended. We strongly advise you to use a join table for this kind of association (as explained in the next section)."
Like I said, hopefully I can be proven wrong. But in the meantime, I have updated my code and schema and am getting onto my next deliverable.