Home » Eclipse Projects » EclipseLink » OnetoOne mapping issue
OnetoOne mapping issue [message #758007] |
Mon, 21 November 2011 03:07 |
pop prem 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 |
|
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
|
|
| | | | | | | | |
Goto Forum:
Current Time: Fri Apr 19 01:03:59 GMT 2024
Powered by FUDForum. Page generated in 0.01688 seconds
|