OnetoOne mapping issue [message #758007] |
Sun, 20 November 2011 22:07  |
Eclipse User |
|
|
|
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 #1062232 is a reply to message #777521] |
Thu, 06 June 2013 11:47  |
Eclipse User |
|
|
|
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?
|
|
|
Powered by
FUDForum. Page generated in 0.04068 seconds