[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [eclipselink-users] Many-To-Many Remove Problem
|
I think I have the problem solved.
Although I have another case where I am calling an EntityManager merge on a
JPA entity with a collection of objects that are of a many-to-many
relationship .... and I haven't experience this problem, I was able to solve
my problem in this case by using the EntityManager refresh method as
follows:
After I call the EntityManager merge on the updated MediaCategory entity, in
the select method for the MediaCategory I do this now -
MediaCategory mc = em.find(MediaCategory.class, catId);
em.refresh(mc);
That works.
-sonavor
sonavor wrote:
>
> I have a problem with updating a JPA entity where a many-to-many
> relationship is involved. It appears to be some sort of session caching
> issue but I can't find the source of the problem
>
> I can add items to my main entity and see that the many-to-many mapping
> table updates with the results. When I re-select the main entity and
> display the values it shows all of the mapped items. When I remove a
> mapped item and re-select the entity the removed item(s) still show up in
> the select query. However, if I look at the mapping table in the database
> the items did remove correctly.
>
> My case is this -
>
> main table:
> MEDIA_CATEGORY
> category_id integer PK
> cat_description varchar(50)
>
> second table:
> KEYWORDS
> kw_id integer PK
> kw_name varchar(50)
>
> mapping table:
> CATKWMAP
> kw_id integer PK
> category_id integer PK
>
> The MEDIA_CATEGORY JPA entity class is MediaCategory. Here are the column
> to attribute mappings -
>
> @Column(name = "CATEGORY_ID", nullable = false)
> private Integer categoryId;
>
> @Column(name = "CAT_DESCRIPTION")
> private String catDescription;
>
> @JoinTable(name = "CATKWMAP", joinColumns = {@JoinColumn(name =
> "CATEGORY_ID", referencedColumnName = "CATEGORY_ID")}, inverseJoinColumns
> = {@JoinColumn(name = "KW_ID", referencedColumnName = "KW_ID")})
> @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
> private Collection<Keywords> catKwCollection;
>
> In my update code I just populate a MediaCategory class with the
> categoryId, catDescription and a collection of keywords ( catKwCollection
> );
>
> I then tell the EntityManager to merge the changes -
>
> em.merge(mediaCategory);
>
> After that occurs I checked the CATKWMAP table in the database and can see
> that the submitted keyword-to-media_category mappings are there. Whether
> the merge occurs where keywords are added or removed the database table
> ends up with the correct results.
>
> If I add code to check the MEDIA_CATEGORY record right after the merge -
>
> MediaCategory mcUpdated = em.merge(mc);
> Collection<Keywords> kwListing = mcUpdated .getCatKwCollection();
> for (Iterator it = kwListing.iterator(); it.hasNext();) {
> KwList kwl = (KwList) it.next();
> log("updateMediaCategory - keyword: " + kwl.toString());
> }
>
> The output shows keywords that match what the updated table has.
>
> But if I go to a screen where I select the updated MediaCategory ID and
> have my code re-select the record using -
>
> MediaCategory mc = em.find(MediaCategory.class, categoryId);
>
> ...the mc.getCatKwCollection() contains all of the keywords I had prior to
> the update. In other words, if my update removed some keyword
> relationships and the table shows them removed, they still exist in this
> selected MediaCategory instance.
>
> I have to stop my web application, restart it and re-select the
> MediaCategory record in order to be in-sync with the database. What is
> going on here? I tried adding a - em.flush() after my merge but it
> didn't help.
>
> Thanks,
> -sonavor
>
>
>
>
>
>
>
--
View this message in context: http://www.nabble.com/Many-To-Many-Remove-Problem-tp25218190p25218375.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.