devuser
devuser

Reputation: 146

a Different object with the same identifier value was already associated with the session

i'm trying to persist entity (Sortie) where the User are connected. so i have one adminstrator who affect sortie to user but i have this issu when the user are connected means when the object are already in the session. how can i persist the entity Sortie while the whole users are connected. here is my code:

Model Sortie

@Entity
public class Sortie implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private Date dateSortie;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "id_utilisateur", nullable = false)
private Utilisateur utilisateur;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "id_recepteur", nullable = false)
private Utilisateur recepteur;

.... }

DAOs:

AbstarctDao :

public abstract class AbstractDao<PK extends Serializable, T> {

private final Class<T> persistentClass;

@SuppressWarnings("unchecked")
public AbstractDao() {
    this.persistentClass = (Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass())
            .getActualTypeArguments()[1];
}

@Autowired
private SessionFactory sessionFactory;

protected Session getSession() {
    return sessionFactory.getCurrentSession();
}

@SuppressWarnings("unchecked")
public T getByKey(PK key) {
    return (T) getSession().get(persistentClass, key);
}

public void persist(T entity) {
    getSession().persist(entity);

}

public void merge(T entity) {
    getSession().merge(entity);


}

public void update(final T entity) {
    Preconditions.checkNotNull(entity);
    getSession().update(entity);
}

public void updateorsave(final T entity) {
    Preconditions.checkNotNull(entity);
    getSession().saveOrUpdate(entity);
}

public void delete(T entity) {
    getSession().delete(entity);
}

protected Criteria createEntityCriteria() {
    return getSession().createCriteria(persistentClass);
}

}

SortieDaoImp:

 ....
  public Sortie addSortie(Sortie sortie) {
    merge(sortie);
    return sortie;
}
.....

Controller

 ......
 Sortie sortie = new Sortie();
 sortie.setUtilisateur(userService.findUserByUsername(getPseudoCo()));
 sortie.setRecepteur(commande.getUtilisateur());
 Sortie exit = sortieService.addSortie(sortie);
 ......

[Edit] : i can't clear the session because i still need the obejct sortie.

Upvotes: 2

Views: 1565

Answers (1)

devuser
devuser

Reputation: 146

I finally found a solution. so i had to use persist on DAoImplementation like this :

....
public Sortie addSortie(Sortie sortie) {
persist(sortie);
return sortie;
}
.....

And create an update methode which will update sortie by affecting user.

@Override
public void updateSortieRecepteur(Sortie sortie, Utilisateur recepteur) {
    sortie.setRecepteur(recepteur);
    update(sortie);
} 

finally i modify the controller :

 ......
 Sortie sortie = new Sortie();
 sortie.setUtilisateur(userService.findUserByUsername(getPseudoCo()));
 sortie.setRecepteur(commande.getUtilisateur());
 Sortie exit = sortieService.addSortie(sortie);
 sortieService.updateSortieRecepteur(exit, commande.getUtilisateur());
 ......

Upvotes: 1

Related Questions