A different object with the same identifier value was already associated with the session

3

When I edit a reservation I get this error in the console:

  

A different object with the same identifier value was already   associated with the session: [br.blumar.events.model.Route # 3883]

Here is my Reservation class (which I want to edit):

package br.com.blumar.events.model;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
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.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import br.com.blumar.events.model.enumeration.EPaymentStatus;
import br.com.blumar.events.model.enumeration.EReservationOrigin;

@Entity
@Table(name = "transfer_reservation_new")
@SequenceGenerator(name = "seq", sequenceName = "events.transfer_reservation_new_id_seq")
public class TransferReservation implements Serializable {

  private static final long serialVersionUID = -4130238319014109932 L;

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO, generator = "seq")
  private Integer id;

  private String name;

  private String email;

  @ManyToOne
  @JoinColumn(name = "event_id")
  private Event event;

  @ManyToOne
  @JoinColumn(name = "reservation_status_id")
  private ReservationStatus reservationStatus;

  @Column(name = "creation_date", insertable = false, updatable = false)
  private Date creationDate;

  @ManyToOne
  @JoinColumn(name = "country_id")
  private Country country;

  @OrderBy("date ASC")
  @OneToMany(mappedBy = "transferReservation", cascade = CascadeType.ALL, orphanRemoval = true)
  private List<Route>routeList;

  @OneToMany(mappedBy = "transferReservation", cascade = CascadeType.ALL, orphanRemoval = true)
  private List<TransferReservationPayment>paymentList;

  @OneToMany(mappedBy = "transferReservation", cascade = CascadeType.ALL, orphanRemoval = true)
  private List<TransferReservationReversal>reversalList;

  @ManyToOne
  @JoinColumn(name = "currency_id")
  private Currency currency;

  @Column(name = "reservation_origin_id")
  private Integer reservationOrigin = EReservationOrigin.SITE.getId();

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }

  public Event getEvent() {
    return event;
  }

  public void setEvent(Event event) {
    this.event = event;
  }

  public List<Route>getRouteList() {
    return routeList;
  }

  public void setRouteList(List<Route>routeList) {
    this.routeList = routeList;
  }

  public ReservationStatus getReservationStatus() {
    return reservationStatus;
  }

  public void setReservationStatus(ReservationStatus reservationStatus) {
    this.reservationStatus = reservationStatus;
  }

  public Date getCreationDate() {
    return creationDate;
  }

  public void setCreationDate(Date creationDate) {
    this.creationDate = creationDate;
  }

  public Country getCountry() {
    return country;
  }

  public void setCountry(Country country) {
    this.country = country;
  }

  public List<TransferReservationPayment>getPaymentList() {
    return paymentList;
  }

  public void setPaymentList(List<TransferReservationPayment>paymentList) {
    this.paymentList = paymentList;
  }

  public List<TransferReservationReversal>getReversalList() {
    return reversalList;
  }

  public void setReversalList(List<TransferReservationReversal>reversalList) {
    this.reversalList = reversalList;
  }

  public Integer getReservationOrigin() {
    return reservationOrigin;
  }

  public void setReservationOrigin(Integer reservationOrigin) {
    this.reservationOrigin = reservationOrigin;
  }

  public Double getTotalPrice() {
    Double totalPrice = new Double(0);

    for (Route route: this.getRouteList())
      totalPrice += route.getPrice();

    return totalPrice;
  }

  public Currency getCurrency() {
    return currency;
  }

  public void setCurrency(Currency currency) {
    this.currency = currency;
  }

  public boolean isOnlyOnePaymentConfirmed() {
    int confirmedPaymentQtt = 0;
    for (TransferReservationPayment payment: paymentList) {
      if (payment.getPaymentStatus().getId().equals(EPaymentStatus.PAID.getId()))
        confirmedPaymentQtt++;
    }
    return confirmedPaymentQtt == 1;
  }

  public TransferReservationPayment getPaymentConfirmed() {
    for (TransferReservationPayment payment: paymentList) {
      if (payment.getPaymentStatus().getId().equals(EPaymentStatus.PAID.getId()))
        return payment;
    }
    return null;
  }

  public boolean isAtLeastOneCreditcardPaymentConfirmed() {
    for (TransferReservationPayment payment: paymentList) {
      if (payment.getPaymentStatus().getId().equals(EPaymentStatus.PAID.getId()) &&
        (payment instanceof TransferReservationCreditcardPayment ||
          payment instanceof TransferReservationCharginglinkPayment))
        return true;
    }
    return false;
  }

  public Double getTotalPayment() {
    BigDecimal total = new BigDecimal(BigInteger.ZERO);

    for (TransferReservationPayment payment: this.getPaymentList()) {
      if (payment.getPaymentStatus().getId().equals(EPaymentStatus.PAID.getId()))
        total = total.add(BigDecimal.valueOf(payment.getValue()));
    }

    return total.doubleValue();
  }

  public Double getTotalReversal() {
    BigDecimal total = new BigDecimal(BigInteger.ZERO);

    for (TransferReservationReversal reversal: this.getReversalList())
      if (reversal.getNetValue() != null)
        total = total.add(BigDecimal.valueOf(reversal.getNetValue()));

    return total.doubleValue();
  }
}

And here's my Route class (which is apparently giving error):

package br.com.blumar.events.model;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
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.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import br.com.blumar.events.model.enumeration.EAdditionalInfo;

@Entity
@Table(name = "route")
@SequenceGenerator(name = "seq", sequenceName = "events.route_id_seq")
public class Route implements Serializable {
  private static final long serialVersionUID = 2132155173071197885 L;

  @Id
  @GeneratedValue(generator = "seq", strategy = GenerationType.IDENTITY)
  private Integer id;

  @ManyToOne
  @JoinColumn(name = "transfer_reservation_id")
  private TransferReservation transferReservation;

  @ManyToOne
  @JoinColumn(name = "id_from")
  private TransferLocal from;

  @ManyToOne
  @JoinColumn(name = "id_to")
  private TransferLocal to;

  private Double price;

  @Column(name = "pax_number")
  private Integer paxNumber;

  @OneToMany(mappedBy = "route", cascade = CascadeType.ALL, orphanRemoval = true)
  private List<AgeChd>ageChdList;

  @Column(name = "special_needs")
  private String specialNeeds;

  private String observation;

  @ManyToOne
  @JoinColumn(name = "transportation_id")
  private Transportation transportation;

  @OneToMany(mappedBy = "route", cascade = CascadeType.ALL, orphanRemoval = true)
  private List<AdditionalInfo>addtionalInfoList;

  private String hour;

  private Date date;

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public TransferReservation getTransferReservation() {
    return transferReservation;
  }

  public void setTransferReservation(TransferReservation transferOptionReservation) {
    this.transferReservation = transferOptionReservation;
  }

  public TransferLocal getFrom() {
    return from;
  }

  public void setFrom(TransferLocal from) {
    this.from = from;
  }

  public TransferLocal getTo() {
    return to;
  }

  public void setTo(TransferLocal to) {
    this.to = to;
  }

  public Double getPrice() {
    return price;
  }

  public void setPrice(Double price) {
    this.price = price;
  }

  public Integer getPaxNumber() {
    return paxNumber;
  }

  public void setPaxNumber(Integer paxNumber) {
    this.paxNumber = paxNumber;
  }

  public List<AgeChd>getAgeChdList() {
    return ageChdList;
  }

  public void setAgeChdList(List<AgeChd>ageChdList) {
    this.ageChdList = ageChdList;
  }

  public String getSpecialNeeds() {
    return specialNeeds;
  }

  public void setSpecialNeeds(String specialNeeds) {
    this.specialNeeds = specialNeeds;
  }

  public String getObservation() {
    return observation;
  }

  public void setObservation(String observation) {
    this.observation = observation;
  }

  public Transportation getTransportation() {
    return transportation;
  }

  public void setTransportation(Transportation transportation) {
    this.transportation = transportation;
  }

  public List<AdditionalInfo>getAddtionalInfoList() {
    return addtionalInfoList;
  }

  public void setAddtionalInfoList(List < AdditionalInfo > addtionalInfoList) {
    this.addtionalInfoList = addtionalInfoList;
  }

  public Integer getTotalPaxNumber() {
    return this.getPaxNumber() + this.getPaxNumberChd();
  }

  public Integer getPaxNumberChdToPay() {
    Integer paxNumberChdToPay = 0;

    if ((this.getAgeChdList() != null) && (!this.getAgeChdList().isEmpty()))
      for (AgeChd age: this.getAgeChdList())
        if (age != null && age.getAge() > 2)
          paxNumberChdToPay += 1;

    return paxNumberChdToPay;
  }

  public Integer getPaxNumberChd() {
    return this.getAgeChdList() == null ? 0 : this.getAgeChdList().size();
  }

  public String getHour() {
    return hour;
  }

  public void setHour(String hour) {
    this.hour = hour;
  }

  public Date getDate() {
    return date;
  }

  public void setDate(Date date) {
    this.date = date;
  }

  public AdditionalInfo getInfoByType(EAdditionalInfo type) {
    for (AdditionalInfo additionalInfo: this.addtionalInfoList)
      if (additionalInfo.getType().equals(type.getId()))
        return additionalInfo;

    return new AdditionalInfo();
  }
}

It's worth mentioning that I'm already using session.clear() before changing it and it does not seem to be this.

    
asked by anonymous 13.08.2018 / 21:46

1 answer

1

The problem was in my JSP, more specifically in this line:

<input type="hidden" id="routeId" complement-data="].id" name="" value="${route.id}" class="validateList">

This line sent the Object Id (Route) to the controller. And in the controller, it does a get with hibernate loading the buffer and consequently it loads the route again and then there are 2 routes with the same ID.

I just removed this line.

    
15.08.2018 / 16:09