Hibernate version:3.2.1 (JPA)
Mapping documents:
Code:
@Entity
@Table(name = "T_PARTICIPANT")
@SequenceGenerator(name = "S_PARTICIPANT", sequenceName = "S_PARTICIPANT)
public class Participant implements Serializable {
/** Primary key. */
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "S_PARTICIPANT")
@Column(name = "PARTICIPANT_ID")
private Integer participantId;
....
}
Code:
@Entity
@Table(name = "T_REQUEST")
@SequenceGenerator(name = "S_REQUEST", sequenceName = "S_REQUEST")
public class Request implements Serializable, AuthorizationVisitable {
private static final long serialVersionUID = -8876974983488983532L;
/** Primary key. */
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "S_REQUEST")
@Column(name = "REQUEST_ID")
private Integer requestId;
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "REQUEST_ID")
private Set<Participant> participants;
...
}
Code between sessionFactory.openSession() and session.close():Code:
@Transactional
public Request saveRequest(Request request) {
for (Participant participant : request.getParticipant()) {
if (participant.getParticipantId() == null || participant.getParticipantId() == 0) {
em.persist(participant);
} else {
participant = em.merge(participant);
}
}
//code
if (request.getRequestId() == null || request.getRequestId() == 0) {
em.persist(request);
} else {
request = em.merge(request);
}
}
return request;
}
Full stack trace of any exception that occurs:
Unable to find model.Participant with id XXXX
Name and version of the database you are using: Oracle 10G
Hi,
I try to persist a list of Participants then the Request that contains this list.
If a rollback is called between the two persist, the list of participants is not stored in database but their id have the value of the generated primary key.
The problem occurs when I try to call this method a second time. The list of participant is not persisted but has a not null primary key.
What is wrong in my code then?
Thanks in advance
Py