Reputation: 1420
USER MAPPING
@Id
@Basic(optional = false)
@SequenceGenerator(name = "sec_t_usuario", allocationSize = 1)
@GeneratedValue(generator = "sec_t_usuario", strategy = GenerationType.SEQUENCE)
@Column(name = "id")
private Integer id;
@OneToMany(mappedBy = "idUsuario", cascade = {CascadeType.ALL})
private Set<Session> sessions;
SESSION MAPPING
@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.REFRESH })
@JoinColumn(name = "id_usuario", referencedColumnName = "id", nullable = false)
private User user;
CODE LINKING TWO ENTITIES
@Transactional(propagation=Propagation.REQUIRES_NEW, rollbackFor=PlayTheGuruException.class)
private Session createSession(final User user) throws PlayTheGuruException
{
final Session session = new Session();
session.setActive(true);
session.setLastLogin(Calendar.getInstance());
session.setUser(user);
user.addSession(session);
entityManager.merge(user);
return session;
}
MAIN METHOD
public Message<ResultDTO> registerUser(final Message<NewUserDataDTO> messageIn) throws PlayTheGuruException
{
.
.
.
final Session session = createSession(messageIn);
.
//here the user Id is still null
.
Having this context, why when I return from createSession transactional method the user id is still null knowing that the transaction has been commited?
I have the queries log from eclipse links and show me that user is inserted and the id is assigned too but seems has not been refreshed in the code.
I have tried flush and get object user from session or directly and not works.
Thanks!
Upvotes: 0
Views: 1110
Reputation: 4623
The entityManager.merge()
(see here) method returns a managed instance. It seems to me that the user
instance sent as parameter is not being attached. In other words, it's not what you'd call an "out" parameter, so you won;t have the ID set in that object. You need to use the returned object in order to find the generated ID.
The session
instance is definitely not managed. And also its referenced User
is not a managed instance.
Upvotes: 1