Hi Everyone,
I have some trouble with cascading on a one to many relationship with Hibernate 3.5.1-Final.
It is a One To Many Relationship between Faculty 1 ---- N Group.
The cascade works well on .save() method call. But it fails on .merge()
This is my testcase:
Code:
@Test
public void shouldCascadeWithGroupsWithAlreadyExistingFaculty()
{
Faculty f = new Faculty();
f.setName("FH");
this.entityManager.persist(f);
this.entityManager.flush();
this.entityManager.clear();
final Group g1 = new Group();
g1.setName("Foobar");
// associate both
g1.setFaculty(f);
f.getGroups().add(g1);
this.entityManager.merge(f);
Assert.assertNotNull(g1.getId());
this.entityManager.flush();
this.entityManager.clear();
f = this.cut.readByPrimaryKey(f.getId());
Assert.assertEquals("The two groups should be persited!", 1, f.getGroups().size());
}
The entities look like this:
Code:
@Entity(name = "Groups")
@Table(uniqueConstraints = { @UniqueConstraint(columnNames = { "faculty_id", "name" }) })
public class Group implements Serializable
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne(optional = false, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH })
private Faculty faculty;
@Length(min = 1, max = 80)
private String name;
@OneToMany(mappedBy = "group")
private List<Timetable> timetables = new ArrayList<Timetable>();
// Getter and Setter omitted
}
Code:
@Entity
public class Faculty implements Serializable
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(unique = true)
@Length(min = 1, max = 80)
private String name;
@OneToMany(mappedBy = "faculty", cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH, CascadeType.REMOVE })
private List<Group> groups = new ArrayList<Group>();
// Getter and Setter omitted
}
Could someone give me a Tipp why?
Thanks a lot.