Cześć
Mam klasę:
@Entity
@Table(name = "unit")
public class Unit implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "unit_id"unique = true)
private int unitId;
@Column(name = "name", nullable = false)
private String name;
//...
}
@Entity
@Table(name = "ticket")
public class Ticket implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ticket_id")
private int ticketId;
@ManyToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "send_unit_id")
private Unit senderUnit;
@ManyToOne(cascade = {CascadeType.ALL})
@JoinColumn(name = "recieve_unit_id")
private Unit recieverUnit;
//...
}
Chcę dodać nowy Ticket z istniejącymi Unit. Robię to tak:
Unit senderUnit = sessionFactory.getCurrentSession().get(Unit.class, id);
Unit recieverUnit = sessionFactory.getCurrentSession().get(Unit.class, id);
Ticket ticket = new Ticket();
ticket.setSenderUnit(senderUnit);
ticket.setRecieverUnit(recieverUnit);
sessionFactory.saveOrUpdate(ticket);
Problem w tym, ze dostaje wyjatek:
NonUniqueObjectException: a different object with the same identifier value was already associated with the session
Czytałem o tym tutaj http://pro-programmers.blogspot.com/2009/03/hibernate-nonuniqueobjectexception.html jednak to nic mi nie pomogło.
Otrzymuje ConstraintViolationException: could not execute statement
Mysle, ze cos zle robie w kodzie, ktory podeslalem powyzej.