Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » OnetoOne mapping issue
OnetoOne mapping issue [message #758007] Mon, 21 November 2011 03:07 Go to next message
pop prem is currently offline pop premFriend
Messages: 20
Registered: June 2010
Junior Member
HI,

I'm having a problem with updating data through OnetoOne mappings as sometimes the data gets saved, sometimes it doesn't but no exceptions are being throwed.

Some parts of my code:

Coupon class:

....

@OneToOne(cascade = CascadeType.ALL, mappedBy="coupon")
private CouponsRatings couponsRatings;

...


CouponRating class:

....

@OneToOne(cascade = CascadeType.MERGE)
@JoinColumn(name="coupon_id", nullable=false, updatable=false)
private Coupon coupon;

...


When creating a coupon, I set an empty CouponRating object which inserts a row in coupon_rating table with id and coupon_id. After that, everytime the rating happens, coupon_rating table directly updated. The update works for 2-3 times and after that the row doesn't get update but no exceptions thrown too.

When i update rating, first i find it through id or coupon id and then set the values to that entity and update. One thing i noted was, sometimes the find returns an old entity with old values even though updated version is in db, so i tried disabling cache and setting hints to find method as below.

Code for find / update methods:

@Override
public CouponsRatings findRatingById(Long id) {
CouponsRatings couponsRatings = null;
try {
entityManager.clear();
couponsRatings = (CouponsRatings) entityManager
.createNamedQuery("findRatingById")
.setHint("eclipselink.read-only", "true")
.setHint("eclipselink.refresh", "true")
.setParameter(1, id).getSingleResult();
} catch (NoResultException exception) {
logger.error("No active couponsRatings available for rating id : " + id);
}
return couponsRatings;
}

@Override
public CouponsRatings findRatingByCouponId(Long id) {
CouponsRatings couponsRatings = null;
try {
entityManager.clear();
couponsRatings = (CouponsRatings) entityManager
.createNamedQuery("findRatingByCouponId")
.setHint("eclipselink.read-only", "true")
.setHint("eclipselink.refresh", "true")
.setParameter(1, id).getSingleResult();
} catch (NoResultException exception) {
logger.error("No active couponsRatings available for coupon id : " + id);
}
return couponsRatings;
}

@Override
public CouponsRatings updateRating(CouponsRatings rating)
throws CouponzleAppException {
CouponsRatings updatedRating = null;
try {
updatedRating = entityManager.merge(rating);
} catch (Exception e) {
logger.debug("Error in updating coupon. Reason: " + e.getMessage());
throw new AppException(e.getMessage());
}
return updatedRating;
}


in Persistence.xml i have these settings too

<property name="eclipselink.cache.shared.default" value="false"/>
<property name="eclipselink.query-results-cache" value="false"/>


Even after doing all these, after few operations i get old entities in find and update method doesn't update the data in the table but no exceptions. Am i doing anything wrong here?
(I'm using Spring transactions for transaction handling, just spring configs for eclipselink and @Transactional annotation in manager layer.)
Re: OnetoOne mapping issue [message #758049 is a reply to message #758007] Mon, 21 November 2011 20:05 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

Try removing,

.setHint("eclipselink.read-only", "true")

This will return read-only objects, if you don't treat these as read-only, and change them, then they can cause your persistence context to be corrupt.

Also be careful with using merge, are you sure you are merging what you think you are, check if you have different copies of the same objects in its relationships, it may be getting merged twice and overwriting the changes.


James : Wiki : Book : Blog : Twitter
Re: OnetoOne mapping issue [message #758050 is a reply to message #758007] Mon, 21 November 2011 20:05 Go to previous messageGo to next message
James is currently offline JamesFriend
Messages: 272
Registered: July 2009
Senior Member
Try removing,

setHint("eclipselink.read-only", "true")

This will return read-only objects, if you don't treat these as read-only, and change them, then they can cause your persistence context to be corrupt.

Also be careful with using merge, are you sure you are merging what you think you are, check if you have different copies of the same objects in its relationships, it may be getting merged twice and overwriting the changes.

--
James : http://wiki.eclipse.org/EclipseLink : http://en.wikibooks.org/wiki/Java_Persistence : http://java-persistence-performance.blogspot.com/
Re: OnetoOne mapping issue [message #758132 is a reply to message #758050] Tue, 22 November 2011 02:33 Go to previous messageGo to next message
pop prem is currently offline pop premFriend
Messages: 20
Registered: June 2010
Junior Member
Thanks a lot for the reply James.

Are you talking merge in @OneToOne(cascade = CascadeType.MERGE) or entityManager.merge? When an entity gets updated, i use entityManager merge method to update the changes to DB and that entity doesn't have any other copies. Even i don't see any overwrites happen with old data in table rows, either the correct updates gets saved or nothing written.

I'll try removing read-only hint. Regarding the CascadeType.MERGE, have i used it correctly? When i update Coupon_Rating table (Child table), i don't want any changes to be done in Coupon table (Parent table), so i have used this. Is that ok?
Re: OnetoOne mapping issue [message #758133 is a reply to message #758049] Tue, 22 November 2011 02:33 Go to previous messageGo to next message
pop prem is currently offline pop premFriend
Messages: 20
Registered: June 2010
Junior Member
Thanks a lot for the reply James.

Are you talking merge in @OneToOne(cascade = CascadeType.MERGE) or entityManager.merge? When an entity gets updated, i use entityManager merge method to update the changes to DB and that entity doesn't have any other copies. Even i don't see any overwrites happen with old data in table rows, either the correct updates gets saved or nothing written.

I'll try removing read-only hint. Regarding the CascadeType.MERGE, have i used it correctly? When i update Coupon_Rating table (Child table), i don't want any changes to be done in Coupon table (Parent table), so i have used this. Is that ok?
Re: OnetoOne mapping issue [message #771963 is a reply to message #758133] Wed, 28 December 2011 16:51 Go to previous messageGo to next message
pop prem is currently offline pop premFriend
Messages: 20
Registered: June 2010
Junior Member

James,

I still stuck with this issue. I have tried removing read-only hint, making bi-directional one to one mapping to uni directional mapping, refreshing the saved entity after updating, adding @cachable(false) to entities and also tried to update without calling merge(). Nothing seems to work. Can you please provide me advice on this.

Thanks.
Re: OnetoOne mapping issue [message #771965 is a reply to message #758133] Wed, 28 December 2011 16:51 Go to previous messageGo to next message
pop prem is currently offline pop premFriend
Messages: 20
Registered: June 2010
Junior Member
James,

I still stuck with this issue. I have tried removing read-only hint, making bi-directional one to one mapping to uni directional mapping, refreshing the saved entity after updating, adding @cachable(false) to entities and also tried to update without calling merge(). Nothing seems to work. Can you please provide me advice on this.

Thanks.
Re: OnetoOne mapping issue [message #777515 is a reply to message #771965] Tue, 10 January 2012 16:12 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

Very odd. Check your code, specifically you get and set methods, maybe your get/set methods have side effects that are causing odd behavior.


James : Wiki : Book : Blog : Twitter
Re: OnetoOne mapping issue [message #777521 is a reply to message #771965] Tue, 10 January 2012 16:12 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

Very odd. Check your code, specifically you get and set methods, maybe your get/set methods have side effects that are causing odd behavior.

--
James : http://wiki.eclipse.org/EclipseLink : http://en.wikibooks.org/wiki/Java_Persistence : http://java-persistence-performance.blogspot.com/


James : Wiki : Book : Blog : Twitter
Re: OnetoOne mapping issue [message #1062232 is a reply to message #777521] Thu, 06 June 2013 15:47 Go to previous message
Dario Galvis is currently offline Dario GalvisFriend
Messages: 1
Registered: June 2013
Junior Member
I'm having the same problem one of our fields is annotated with this

Owner side:
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)

the other side:
@OneToOne(mappedBy = "theOwnerFieldName", fetch = FetchType.LAZY)

Sometimes it doesn't get saved when a merge is applied to the owner side and the other side represent a just created entity.

Pop Prem, may you please share with us what kind of workaround you used?
Previous Topic:DISTINCT and ORDER BY
Next Topic:Validation exception on spurious insert during a query transaction
Goto Forum:
  


Current Time: Thu Apr 18 23:32:31 GMT 2024

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

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

Back to the top