Hi,
I am trying to duplicate an Entity including it's referenced entities (oneToMany). The idea is to have a template which can be copied, used and modified. This Template is "pfadLeistung" which references (oneToMany) "PfadLeistungSachmittelReferenz".  I want to duplicate a "pfadLeistung" including it's references, modify some attributes and persist everything properly. What is the best way to duplicate pfadLeistung including it's references? Following I will post the involved classes and further info's. Thanx
Felix
Hibernate version:3 
Mapping documents:Annotations
Code between sessionFactory.openSession() and session.close():
package prozessManager.services;
import ....
public class HibernateUtil {
	private static ThreadLocal<Session> currentSession = new ThreadLocal<Session>();
	
	public static void setCurrentSession(Session session) {
		currentSession.set(session);
	}
	
	public static Session getCurrentSession() {
		return currentSession.get();
	}
	
	private static boolean recreateDB = false;
	
	public static void setRecreateDB(boolean recreateDB) {
		HibernateUtil.recreateDB = recreateDB;
	}
	
	public static SessionFactory getSessionFactory() {
		if (sessionFactory == null) {
			sessionFactory = sessionFactory("hibernate.cfg.xml");
		}
		return sessionFactory;
	}
	
	private static SessionFactory sessionFactory = null;
	
	private static SessionFactory sessionFactory(String configurationFileName) {
		try {
			AnnotationConfiguration annotationConfiguration = 
				new AnnotationConfiguration()
				.addAnnotatedClass(Test.class)
				.addAnnotatedClass(Pfad.class)
				.addAnnotatedClass(Krankheitsbild.class)
				.addAnnotatedClass(KrankheitsbildPfadReferenz.class)
				.addAnnotatedClass(KrankheitsbildDiagnoseReferenz.class)
				.addAnnotatedClass(Diagnose.class)
				.addAnnotatedClass(Leistung.class)
				.addAnnotatedClass(PfadLeistungReferenz.class)
				.addAnnotatedClass(PfadLeistungBeteiligteOrganisationseinheitReferenz.class)
				.addAnnotatedClass(PfadLeistungSachmittelReferenz.class)
				.addAnnotatedClass(Patient.class)
				.addAnnotatedClass(LeistungTyp.class)
				.addAnnotatedClass(Organisationseinheit.class)
				.addAnnotatedClass(Sachmittel.class)
				.addAnnotatedClass(Mandant.class)
				.addAnnotatedClass(Aufgabe.class)
				.addAnnotatedClass(Projekt.class)
				.addAnnotatedClass(ProjektAufgabeReferenz.class)
				.addAnnotatedClass(ProjektAufgabeBeteiligteOrganisationseinheitReferenz.class)
				.addAnnotatedClass(PatientPfadReferenz.class)
				;
			
			if (recreateDB) annotationConfiguration.setProperty("hibernate.hbm2ddl.auto", "create");
			annotationConfiguration.configure();
			return annotationConfiguration.buildSessionFactory();
		} catch (Throwable ex){
			System.err.println("Initial SessionFactory creation failed." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}
	
}
Name and version of the database you are using:MySQL 5.0
Error Message:
HTTP Status 500 - 
--------------------------------------------------------------------------------
type Exception report
message 
description The server encountered an internal error () that prevented it from fulfilling this request.
exception 
javax.servlet.ServletException: Error calling action method of component with id _idJsp0:_idJsp53
	javax.faces.webapp.FacesServlet.service(FacesServlet.java:154)
	prozessManager.web.HibernateSessionRequestFilter.doFilter(HibernateSessionRequestFilter.java:28)
root cause 
javax.faces.FacesException: Error calling action method of component with id _idJsp0:_idJsp53
	org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:72)
	javax.faces.component.UICommand.broadcast(UICommand.java:109)
	javax.faces.component.UIViewRoot._broadcastForPhase(UIViewRoot.java:97)
	javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:171)
	org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:32)
	org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:95)
	org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:70)
	javax.faces.webapp.FacesServlet.service(FacesServlet.java:139)
	prozessManager.web.HibernateSessionRequestFilter.doFilter(HibernateSessionRequestFilter.java:28)
root cause 
javax.faces.el.EvaluationException: Exception while invoking expression #{pfadController.patientZuordnen}
	org.apache.myfaces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:156)
	org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:61)
	javax.faces.component.UICommand.broadcast(UICommand.java:109)
	javax.faces.component.UIViewRoot._broadcastForPhase(UIViewRoot.java:97)
	javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:171)
	org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:32)
	org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:95)
	org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:70)
	javax.faces.webapp.FacesServlet.service(FacesServlet.java:139)
	prozessManager.web.HibernateSessionRequestFilter.doFilter(HibernateSessionRequestFilter.java:28)
root cause 
java.util.ConcurrentModificationException
	java.util.HashMap$HashIterator.nextEntry(Unknown Source)
	java.util.HashMap$KeyIterator.next(Unknown Source)
	org.hibernate.collection.AbstractPersistentCollection$IteratorProxy.next(AbstractPersistentCollection.java:555)
	prozessManager.controller.pfad.PfadController.pfadLeistungenUebernehmen(PfadController.java:173)
	prozessManager.controller.pfad.PfadController.patientZuordnen(PfadController.java:156)
	sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	java.lang.reflect.Method.invoke(Unknown Source)
	org.apache.myfaces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:132)
	org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:61)
	javax.faces.component.UICommand.broadcast(UICommand.java:109)
	javax.faces.component.UIViewRoot._broadcastForPhase(UIViewRoot.java:97)
	javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:171)
	org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:32)
	org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:95)
	org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:70)
	javax.faces.webapp.FacesServlet.service(FacesServlet.java:139)
	prozessManager.web.HibernateSessionRequestFilter.doFilter(HibernateSessionRequestFilter.java:28)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.23 logs.
--------------------------------------------------------------------------------
Apache Tomcat/5.5.23
SQL: Hibernate: insert into PFADLEISTUNG (anforderndeOrganisationseinheit_fk, bedingung, istVorlage, leistung_fk, patientPfadReferenz_fk, pfad_fk) values (?, ?, ?, ?, ?, ?)
closing session 32870670
The Controller. It's starts with the method "patientZuordnen". The error accrues in line "curPfadLeistungReferenz = (PfadLeistungReferenz)iterator.next();" of the method "pfadLeistungenUebernehmen"
Code:
public class PfadController extends Controller {
...
public String patientZuordnen() {
      PatientPfadReferenz patientPfad = new PatientPfadReferenz();
      pfad.addPatient(patientPfad);
      patientPfad.setPfad(pfad);
      PatientPfadController patientPfadController = new PatientPfadController(patientPfad);
      patientPfadController.getPatientPfad().setPfad(this.pfad);
      
      pfadLeistungenUebernehmen(patientPfad);
      
      save();
      patientPfadController.setPredecessor(this);
      JSFService.getInstance().setController(patientPfadController);
      
      return "patientPfad";
   }
   
   public void pfadLeistungenUebernehmen(PatientPfadReferenz patientPfadReferenz ){
      
      Iterator iterator = this.pfad.getPfadLeistungList().iterator();
      while(iterator.hasNext()){
         
         PfadLeistungReferenz curPfadLeistungReferenz= new PfadLeistungReferenz();
         
         Session session = HibernateUtil.getCurrentSession();
         curPfadLeistungReferenz = (PfadLeistungReferenz)iterator.next();
         session.save(curPfadLeistungReferenz);
         curPfadLeistungReferenz.setIstVorlage(false);
         // This means "is template"
         
         patientPfadReferenz.addLeistung(curPfadLeistungReferenz);
         pfad.removePfadLeistung(curPfadLeistungReferenz);
         // here i am changing some attributes / references
         
         session.update(curPfadLeistungReferenz);
         
      }
   }
...
}
Code:
package prozessManager.domain.pfad;
import....
@Entity
@Table(name = "PFADLEISTUNG")
public class PfadLeistungReferenz {
   private Long id;
   private Pfad pfad;
   private Leistung leistung;
   private Organisationseinheit anforderndeOrganisationseinheit;
   private Set<PfadLeistungBeteiligteOrganisationseinheitReferenz> beteiligteOrganisationseinheiten = new LinkedHashSet<PfadLeistungBeteiligteOrganisationseinheitReferenz>();
   private Set<PfadLeistungSachmittelReferenz> pfadLeistungSachmittel = new LinkedHashSet<PfadLeistungSachmittelReferenz>();
   private PatientPfadReferenz patientPfadReferenz;
   private String bedingung;
   private boolean istVorlage;
   public boolean isIstVorlage() {
      return istVorlage;
   }
   public void setIstVorlage(boolean istVorlage) {
      this.istVorlage = istVorlage;
   }
   @ManyToOne
   @JoinColumn(name="leistung_fk")
   public Leistung getLeistung() {
      return leistung;
   }
   public void setLeistung(Leistung leistung) {
      this.leistung = leistung;
   }
   public PfadLeistungReferenz() {
   }
   
   @Id @GeneratedValue(strategy=GenerationType.AUTO)
   public Long getId() {
      return id;
   }
   public void setId(Long id) {
      this.id = id;
   }
   @ManyToOne
   @JoinColumn(name="pfad_fk")
   public Pfad getPfad() {
      return pfad;
   }
   public void setPfad(Pfad pfad) {
      this.pfad = pfad;
   }
   @ManyToOne()
   @JoinColumn(name="anforderndeOrganisationseinheit_fk")
   public Organisationseinheit getAnforderndeOrganisationseinheit() {
      return anforderndeOrganisationseinheit;
   }
   public void setAnforderndeOrganisationseinheit(
         Organisationseinheit anforderndeOrganisationseinheit) {
      this.anforderndeOrganisationseinheit = anforderndeOrganisationseinheit;
   }
   public String getBedingung() {
      return bedingung;
   }
   public void setBedingung(String bedingung) {
      this.bedingung = bedingung;
   }
   
   public void addAnforderndeOrganisationseinheit(Organisationseinheit organisationseinheit){
      anforderndeOrganisationseinheit=organisationseinheit;
      organisationseinheit.addPfadLeistungAnfordernderList(this);
   }
   
   @OneToMany(mappedBy="pfadLeistung", cascade=CascadeType.ALL,fetch=FetchType.EAGER)
   public Set<PfadLeistungBeteiligteOrganisationseinheitReferenz> getBeteiligteOrganisationseinheiten() {
      return beteiligteOrganisationseinheiten;
   }
   public void setBeteiligteOrganisationseinheiten(
         Set<PfadLeistungBeteiligteOrganisationseinheitReferenz> beteiligteOrganisationseinheiten) {
      this.beteiligteOrganisationseinheiten = beteiligteOrganisationseinheiten;
   }
   
   public void addBeteiligteOrganisationseinheit(Organisationseinheit beteiligteOrganisationseinheit){
      PfadLeistungBeteiligteOrganisationseinheitReferenz ref = new PfadLeistungBeteiligteOrganisationseinheitReferenz();
      ref.setBeteiligteOrganisationseinheit(beteiligteOrganisationseinheit);
      ref.setPfadLeistung(this);
      this.beteiligteOrganisationseinheiten.add(ref);
      
   }
   public void addSachmittel(Sachmittel sachmittel){
      PfadLeistungSachmittelReferenz ref = new PfadLeistungSachmittelReferenz();
      ref.setSachmittel(sachmittel);
      ref.setPfadLeistung(this);
      this.pfadLeistungSachmittel.add(ref);
      
   }
   
   @OneToMany(mappedBy="pfadLeistung", cascade=CascadeType.ALL,fetch=FetchType.EAGER)
   public Set<PfadLeistungSachmittelReferenz> getPfadLeistungSachmittel() {
      return pfadLeistungSachmittel;
   }
   public void setPfadLeistungSachmittel(
         Set<PfadLeistungSachmittelReferenz> pfadLeistungSachmittel) {
      this.pfadLeistungSachmittel = pfadLeistungSachmittel;
   }
   
   public void removeBeteiligteOrganisationseinheit(PfadLeistungBeteiligteOrganisationseinheitReferenz beteiligteOrganisationReferenz){
      this.beteiligteOrganisationseinheiten.remove(beteiligteOrganisationReferenz);
   }
   public void removeSachmittel(PfadLeistungSachmittelReferenz sachmittelReferenz){
      this.pfadLeistungSachmittel.remove(sachmittelReferenz);
   }
   @ManyToOne()
   @JoinColumn(name="patientPfadReferenz_fk")
   public PatientPfadReferenz getPatientPfadReferenz() {
      return patientPfadReferenz;
   }
   public void setPatientPfadReferenz(PatientPfadReferenz patientPfadReferenz) {
      this.patientPfadReferenz = patientPfadReferenz;
   }
   
}
Code:
package prozessManager.domain.pfad;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import prozessManager.domain.sachmittel.Sachmittel;
@Entity
@Table(name = "PfadLeistungSachmittel")
public class PfadLeistungSachmittelReferenz {
   private Long id;
   private Sachmittel sachmittel;
   private PfadLeistungReferenz pfadLeistung;
   private int anzahl;
   
   public int getAnzahl() {
      return anzahl;
   }
   public void setAnzahl(int anzahl) {
      this.anzahl = anzahl;
   }
   @Id @GeneratedValue(strategy=GenerationType.AUTO)
   public Long getId() {
      return id;
   }
   public void setId(Long id) {
      this.id = id;
   }
   
   @ManyToOne
   @JoinColumn(name="pfadLeistung_fk")
   public PfadLeistungReferenz getPfadLeistung() {
      return pfadLeistung;
   }
   public void setPfadLeistung(PfadLeistungReferenz pfadLeistung) {
      this.pfadLeistung = pfadLeistung;
   }
   
   @ManyToOne
   @JoinColumn(name="sachmittel_fk")
   public Sachmittel getSachmittel() {
      return sachmittel;
   }
   public void setSachmittel(Sachmittel sachmittel) {
      this.sachmittel = sachmittel;
   }
}
Code:
package prozessManager.domain.patient;
import java.util.LinkedHashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import prozessManager.domain.pfad.Pfad;
import prozessManager.domain.pfad.PfadLeistungReferenz;
@Entity
@Table(name = "PatientPfad")
public class PatientPfadReferenz {
   private Long id;
   private Pfad pfad;
   private Patient patient;
   private String aufnahmeDatum;
   private Set<PfadLeistungReferenz> pfadLeistungReferenz = new LinkedHashSet<PfadLeistungReferenz>();
   
   public String getAufnahmeDatum() {
      return aufnahmeDatum;
   }
   public void setAufnahmeDatum(String aufnahmeDatum) {
      this.aufnahmeDatum = aufnahmeDatum;
   }
   @ManyToOne
   @JoinColumn(name="patient_fk")
   public Patient getPatient() {
      return patient;
   }
   public void setPatient(Patient patient) {
      this.patient = patient;
   }
   
   @Id @GeneratedValue(strategy=GenerationType.AUTO)
   public Long getId() {
      return id;
   }
   public void setId(Long id) {
      this.id = id;
   }
   @ManyToOne
   @JoinColumn(name="pfad_fk")
   public Pfad getPfad() {
      return pfad;
   }
   public void setPfad(Pfad pfad) {
      this.pfad = pfad;
   }
   
   @OneToMany(mappedBy="patientPfadReferenz", cascade=CascadeType.ALL,fetch=FetchType.EAGER)
   public Set<PfadLeistungReferenz> getPfadLeistungReferenz() {
      return pfadLeistungReferenz;
   }
   public void setPfadLeistungReferenz(
         Set<PfadLeistungReferenz> pfadLeistungReferenz) {
      this.pfadLeistungReferenz = pfadLeistungReferenz;
   }
   
   public void addLeistung (PfadLeistungReferenz pfadLeistungReferenz){
      this.pfadLeistungReferenz.add(pfadLeistungReferenz);
      pfadLeistungReferenz.setPatientPfadReferenz(this);
      
   }
   
   public void removeLeistung (PfadLeistungReferenz pfadLeistungReferenz){
      this.pfadLeistungReferenz.remove(pfadLeistungReferenz);
      pfadLeistungReferenz.setPatientPfadReferenz(null);
      
   }
   
}