Hi all,
this issue is probably extremly obvious and/or stupid, but I just can't figure it out.
I got a small project with two Entites, below is the condensed code:
Code:
@Entity
@Table(name="GAMES")
public class GameEntity extends JavaEntity{
.....
@Column(name="GAME_TEAM1", nullable=false)
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
private TeamEntity team1;
@Column(name="GAME_TEAM2", nullable=false)
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
private TeamEntity team2;
.....
}
@Entity
@Table(name="TEAMS")
public class TeamEntity extends JavaEntity{
TeamEntity(){}
@Id @GeneratedValue
@Column(name="TEAM_ID", nullable=false, unique=true)
private long id;
....
}
And here is the code where I persist an entity (it is from a JavaFX class that's why the declarations look a bit odd):
Code:
var em:EntityManager = EMFactoryEnum.Factory.factory.createEntityManager();
var tx : EntityTransaction = em.getTransaction();
tx.begin();
em.persist(em.merge(entity));
tx.commit();
em.close();
Persisting the TeamEntity works fine. But when I try to persist a GameEntity i get an exception saying "object references an unsaved transient instance - save the transient instance before flushing".
From my knowledge merging and then persisting an entity should be enough to write the entity to the database (and this works for the TeamEntity) and setting the cascade types to persist and merge should ensure that when trying to persist the GameEntity the referenced TeamEntities are persisted as well. At the moment the TeamEntites are not modified when the GameEntity is persisted, but that should not make a difference, should it?
Can someone tell me what I am missing here?
Thanks, Chris