2

Detached entity passed to persist turned my life into hell, it's been two days since i got this problem and I've tried everything in this website and nothing works. So i have two tables "modules" and "attestations", and i have a list of module's titles and what i'm doing is taking that title and retrieve the module and for each module i want to put the attestations that i already created. in my example i'm using ManyToMany relationship

so i have theses following classes :

classe attestations :

@Entity
@Table(name = "edep_attestations")
public class Attestation implements Serializable {
private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
       private Long id;
       private String title;
       ...
       @ManyToMany(mappedBy="attestations")
       private Set<Module> modules = new HashSet<>();

       // getters and setters
}

classe modules :

@Entity
@Table(name = "edep_modules")
public class Module implements Serializable {
     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     protected Long id;
     private String title; 
     ...
     @ManyToMany(fetch = FetchType.EAGER,cascade = {CascadeType.MERGE})
     private Set<Attestation> attestations = new HashSet<>();
    // getters and setters 
}

and this is the method that i'm using in my bean

public String create(){
    Module m = null;
    this.attestationsService.create(attestation);
    for (String title : moduleTitles) {
            m = modulesService.getModulebyTitle(title);
            m.getAttestation().add(attestation); 
            this.modulesService.create(m);
    }
    return "success";
}

class modulesService, create method:

public void create(E object) throws Exception {
    em.clear();
    em.persist(object);
    em.flush();
}
  • Well, you create he same attestation multiple times. Why? – JB Nizet Jun 19 '16 at 16:48
  • my mistake i edited the code to the original – Mourad Lamkas Jun 19 '16 at 16:55
  • Your code is unclear. We have no idea where the attestation comes from, and what the services do. The biggest mistake anyway is that you should have a transactional service doing all of this, instead of getting detached objects then reattaching them. If an exception fails in the middle, half of the work is done, and half of it isn't, leaving the database in an inconsistent state. Work with attached objects, and make sure your code is transactional. The code will be simpler, and faster, too. – JB Nizet Jun 19 '16 at 16:58
  • i have a form with multiple inputs, to enter the information about my attestation (attestation is a managed bean) and i have also an input to enter multiple title of different modules so it's a that return a table of string, so attestation comes from the form and also moduleTitles. – Mourad Lamkas Jun 19 '16 at 17:09
  • Possible duplicate of [JPA/Hibernate: detached entity passed to persist](http://stackoverflow.com/questions/13370221/jpa-hibernate-detached-entity-passed-to-persist) – Neil Stockton Jun 19 '16 at 17:49
  • it's different i'm using manytomany relationship and i tried changing the cascade type nothing works :\ – Mourad Lamkas Jun 19 '16 at 17:55

1 Answers1

2

I solved this by removing the @GeneratedValue annotation. It looks like no other option was working for me (auto or identity).
So what I do instead: I retrieve the max id of my table, increment it by one, and set it manually to my entity. When I then persist the object it's working perfectly.

Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135