-->
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.  [ 2 posts ] 
Author Message
 Post subject: OneToMany.Persist will fails because of a detached reference
PostPosted: Wed Jul 06, 2011 9:06 am 
Newbie

Joined: Wed Jul 06, 2011 4:51 am
Posts: 2
Hi again,
I have some trouble with calling .persist(). This will lead on the following testcase into this Exception: org.hibernate.PersistentObjectException: detached entity passed to persist: Faculty

The two comments will fix the exception but i don't like the way they to this.

Code:
public class CreateTimetableTest
{
   @PersistenceContext
   EntityManager   entityManager;

   @Test
   public void shouldStoreFacultyAndGroupInTwoDifferentTransactions()
   {
      final Faculty f = new Faculty("Foo");
      this.entityManager.persist(f);

      this.entityManager.flush();
      this.entityManager.clear();

      // This will fix the exception by attaching the faculty again to the current session
      // f = this.entityManager.find(Faculty.class, f.getId());

      final Group g = new Group("Bar");

      g.setFaculty(f);
      f.getGroups().add(g);

      // or using merge on instead of persist will fix the exception
      this.entityManager.persist(g);

   }


The Tests simulate the behavior of the web application. On the first Request the Faculty will stored and on the secound the new group. Thats the reason why i call Flush and Clear after persisting the Faculty.


Does anybody know how to solve this?


The Entities are quite simple:
Code:
@Entity(name = "Groups")
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;

   // 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
}


Top
 Profile  
 
 Post subject: Re: OneToMany.Persist will fails because of a detached reference
PostPosted: Fri Jul 08, 2011 10:35 am 
Newbie

Joined: Thu Apr 21, 2011 8:59 am
Posts: 14
you can look at the following lines in your code,
Code:
   @ManyToOne(optional = false, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH })
   private Faculty         faculty;

So, a persist operation on group entity the persist operation will get cascaded to the faculty entity as well.
However, the line
Code:
this.entityManager.clear();
will detach all the earlier entities which were in the persistence context which means faculty f.
So, when a persist on g is called, it tries to persist f as well which in your case is already detached. So, this is resulting in the exception.
From the JPA spec,
Quote:
If X is a detached object, the EntityExistsException may be thrown when the persist
operation is invoked, or the EntityExistsException or another PersistenceException
may be thrown at flush or commit time.

You have to either reload f into the context before the persist is invoked on g.
OR you can remove the cascade or PERSIST.

_________________
Lokesh, C
( SCBCD 5, CCENT, SCJP 5 )


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

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.