[jboss-user] [JBoss Seam] - how to dis-manage entities?

gsegura do-not-reply at jboss.com
Thu Dec 13 01:00:18 EST 2007

Hello, I got into this kind of chicken-egg problem related to entity versioning: 

There is an entity Course which has a collection of CourseNote.
When adding a CourseNote to the collection I have to obtain a managed instance of Course, whose version field is been incremented implicitly when the flush occurs (because it is been managed).

And that is the problem because when more than one user are inserting CourseNotes to the collection, the second one will get an optimisticlockexception at the time he tries to merge his instance of the course

I do need to access the course during CourseNote creation to do some work (so entityManager.getReference is not enough) but  that very action is causing the course to get managed and involved in the transaction, which I do not want to happen.

//validation omited
  | Ticket ticket = (Ticket)entityManager
  | 	.createQuery("select t from Ticket t where t.code=#{register.code}")
  | 	.getSingleResult();				
  | CourseNote courseNote = ticket.getCourse().enroll(user) ;            
  | entityManager.persist(courseNote) ;
  | //...
  | entityManager.flush() ; 

class Course {
  | 	public void enroll(User user) {
  | 		CourseNote cn = new CourseNote(user,this) ;
  | 		if(!getCourseNotes().contains(c)) {
  | 			getCourseNotes().add(c) ;
  | 			usuario.getCourseNotes().add(c) ;
  | 			return c ;
  | 		}
  | 		throw new IllegalStateException(
  | 			new StringBuilder(50)
  | 			.append("User ")
  | 			.append(user.getId())
  | 			.append(" is already enrolled in the course ")
  | 			.append(this.getId()).toString()) ;
  | 	}
  | }

I kindly request some pattern or tip.
Please, some advice on this will be very very appreciated

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4112449#4112449

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4112449

More information about the jboss-user mailing list