-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Cascade on merge
PostPosted: Wed Jul 06, 2011 5:04 am 
Newbie

Joined: Wed Jul 06, 2011 4:51 am
Posts: 2
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.