-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: JPA: Transient References not resolved by Cascade
PostPosted: Thu Oct 08, 2009 7:06 pm 
Newbie

Joined: Thu Oct 08, 2009 7:02 pm
Posts: 1
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.