Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Problems with ManyToOne and OneToMany

Hi,
@Chris: Yes this is what was my solution so far, to update the collections on add and remove manually. But this is quite ugly :-( Using @PostRemove or @PreRemove also didn't really help and caused ugly problems from time to time. For hibernate there is a special non-jpa-standard annotation (I think it could be "inverse", but I'm not quite sure and I couldn't find the site I read about this again) to handle this. If I understood you right, there is no such thing for EclipseLink :-(

@Joe: I think the Cascade.Remove doesn't really help. As mentioned, my database get's updated the right way. I couldn't see any effect on my issue when using the @cascade annotation unfortunately.

Any way, thanks for your help. I fear I'll have to do the updates of the collections manually :-(

-Johannes

2009/7/13 <christopher.delahunt@xxxxxxxxxx>

Hello Johannes,

 

JPA does not maintain relationships for you, and instead requires applications to maintain both sides of every relationship so that they remain consistent with what is in the database.  What that means here is that when you are deleting a Player, you also need to remove any references that might remain in the object model to the deleted player - in this case remove the player from the players set.  If not, Team will reference the removed player until it gets refreshed. 

 

The application has the same issue when you add a player to a team - the application must set both sides of the relationship, or the object model may not reflect what gets set in the database. 

 

Hope this helps.

Chris


----- Original Message -----
From: "Orgler" <orgler@xxxxxxxxx>
To: "Eclipselink-Users" <eclipselink-users@xxxxxxxxxxx>
Sent: Sunday, July 12, 2009 7:19:39 AM GMT -05:00 US/Canada Eastern
Subject: [eclipselink-users] Problems with ManyToOne and OneToMany

Hi,
 
I'm using eclipselink to do some persistence. I'm having the following classes/tables:
 
@Entity
public class Team{
    @OneToMany(mappedBy="team")
    public Set<Player> players;
}

@Entity
public class Player{
    @ManyToOne
    public Team team;
}
 
In my database there is a table team and a table player (with a foreign-key-relationship to a team-ID).
 
This works as far as I'm not deleting anything. But when calling em.remove(myPlayer) the "players"-Set in "Team" is not automatically updated. When closing my application and restarting it, everything works fine. But without this restart the "players"-array seems to get cached somewhere.
 
Any idea on how to solve this? From time to time I'm getting errors that there are unpersistet instances after such a remove operation. For hibernate, I've read something about an "inverse"-annotation which could (if I understood it right) solve such a problem.
 
Any help would be highly appreciated,
 
best regards
-Johannes
 

_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users



Back to the top