Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Update manytomany results in duplicate key exception
Update manytomany results in duplicate key exception [message #629336] Mon, 27 September 2010 20:03 Go to next message
dreske Missing name is currently offline dreske Missing nameFriend
Messages: 17
Registered: August 2010
Junior Member
Hello,

I have a bi-directional many-to-many relationship (User -> Course).
If I add a course to the users courses list and then add the user to the courses users list, I get a duplicate key exception.

If I only add the user to the courses users list, the users courses list is not updated til I manually call refresh for the user?

Is this the right way?
I've read, that I allways should update both sides of the relationship. But how?

thanks
Dirk
Re: Update manytomany results in duplicate key exception [message #629539 is a reply to message #629336] Tue, 28 September 2010 16:00 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
Hello,

That is correct. I believe the problem is you have not used the mappedby on one side of the bi-directional relationship, and so have created two independent relationships that happen to use the same relation table. This means that when you update both sides, both relationships will cause an update to the relation table. The cache is not updated automatically unless a refresh is issued, so if you update one side of a relationship, these changes are never reflected on the other side unless a refresh is done.

If this is the case, the solution is to mark one side as mappedby the other. This will allow the application to update both relationships, with only the changes to the owning side getting persisted to the relation table.

Best Regards,
Chris
icon7.gif  Re: Update manytomany results in duplicate key exception [message #629555 is a reply to message #629539] Tue, 28 September 2010 17:02 Go to previous messageGo to next message
dreske Missing name is currently offline dreske Missing nameFriend
Messages: 17
Registered: August 2010
Junior Member
Yeah, that was it.

Thanks!
Re: Update manytomany results in duplicate key exception [message #682338 is a reply to message #629336] Fri, 10 June 2011 21:14 Go to previous messageGo to next message
Mi?osz  osobucki is currently offline Mi?osz osobuckiFriend
Messages: 1
Registered: June 2011
Junior Member
I have similar problem. However I have one side of relationship mapped:

@Entity
public class UserEntity implements Serializable {
    /*...*/
    @ManyToMany(mappedBy = "owners")
    private List<AppEntity> ownedApps;
    
    public List<AppEntity> getOwnedApps() {
        return ownedApps;
    }

    /*...*/
}


@Entity
public class AppEntity implements Serializable {
    /*...*/
    @ManyToMany
    private List<UserEntity> owners;

    public void addOwner(UserEntity owner) {
        owners.add(owner);
    }

    public List<UserEntity> getOwners() {
        return owners;
    }

    /*...*/
}




In my startup Bean, that is invoked on app startup I have something like that:

@Singleton
@Startup
public class ConfigBean {
    /*...*/

    @PersistenceContext
    private EntityManager em;

    @PostConstruct
    public void createData() {
        /*...*/
        UserEntity admin = new UserEntity();
        em.persist(admin);
        AppEntity someapp = new AppEntity();
        em.persist(somegame);
        somegame.addOwner(admin);
        /*...*/
    }
}


Where em is EntityManager managed by web container.

Now interesting things happen. I'm running this on Glassfish 3. After starting the app, someapp.getOwners() returns list with one element, admin UserEntity, as intended. However, admin.getOwnedApps() returns empty list.

If I disable and then enable the app in Glassfish, admin.getOwnedApps() runs properly and returns list with someapp.

The most interesting thing is however, that if I run Glassfish in debug mode (I start it from NetBeans) i get proper output from admin.getOwnedApps() without disabling and reenabling the app.

It seems that some weird caching is present in normal mode and not in debug mode. What should I do to be sure, that it runs properly from the beginning?

(Adding em.flush() after somegame.addOwner(admin); doesn't work.)
(no subject) [message #682339 is a reply to message #629336] Fri, 10 June 2011 21:14 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by:

I have similar problem. However I have one side of relationship mapped:


@Entity
public class UserEntity implements Serializable {
/*...*/
@ManyToMany(mappedBy = "owners")
private List<AppEntity> ownedApps;

public List<AppEntity> getOwnedApps() {
return ownedApps;
}

/*...*/
}



@Entity
public class AppEntity implements Serializable {
/*...*/
@ManyToMany
private List<UserEntity> owners;

public void addOwner(UserEntity owner) {
owners.add(owner);
}

public List<UserEntity> getOwners() {
return owners;
}

/*...*/
}




In my startup Bean, that is invoked on app startup I have something like that:


@Singleton
@Startup
public class ConfigBean {
/*...*/

@PersistenceContext
private EntityManager em;

@PostConstruct
public void createData() {
/*...*/
UserEntity admin = new UserEntity();
em.persist(admin);
AppEntity someapp = new AppEntity();
em.persist(somegame);
somegame.addOwner(admin);
/*...*/
}
}


Where em is EntityManager managed by web container.

Now interesting things happen. I'm running this on Glassfish 3. After starting the app, someapp.getOwners() returns list with one element, admin UserEntity, as intended. However, admin.getOwnedApps() returns empty list.

If I disable and then enable the app in Glassfish, admin.getOwnedApps() runs properly and returns list with someapp.

The most interesting thing is however, that if I run Glassfish in debug mode (I start it from NetBeans) i get proper output from admin.getOwnedApps() without disabling and reenabling the app.

It seems that some weird caching is present in normal mode and not in debug mode. What should I do to be sure, that it runs properly from the beginning?

(Adding em.flush() after somegame.addOwner(admin); doesn't work.)
Re: (no subject) [message #683422 is a reply to message #682338] Mon, 13 June 2011 17:45 Go to previous message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

In general you have to maintain bi-directional relationships, otherwise your object model will be out of date.

The issue probably works in the debug mode because the default cache type uses soft reference which are probably getting garbage collected.


James : Wiki : Book : Blog : Twitter
(no subject) [message #683441 is a reply to message #682338] Mon, 13 June 2011 17:45 Go to previous message
James is currently offline JamesFriend
Messages: 272
Registered: July 2009
Senior Member
In general you have to maintain bi-directional relationships, otherwise your object model will be out of date.

The issue probably works in the debug mode because the default cache type uses soft reference which are probably getting garbage collected.

--
James : http://wiki.eclipse.org/EclipseLink : http://en.wikibooks.org/wiki/Java_Persistence : http://java-persistence-performance.blogspot.com/
Previous Topic:(no subject)
Next Topic:How can I get count of distinct left join subquery?
Goto Forum:
  


Current Time: Fri Apr 19 15:28:14 GMT 2024

Powered by FUDForum. Page generated in 0.01558 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top