How to Fix the Hibernate "Object References an Unsaved Transient Instance - Save the Transient Instance Before Flushing" Error

How to fix the Hibernate object references an unsaved transient instance - save the transient instance before flushing error

You should include cascade="all" (if using xml) or cascade=CascadeType.ALL (if using annotations) on your collection mapping.

This happens because you have a collection in your entity, and that collection has one or more items which are not present in the database. By specifying the above options you tell hibernate to save them to the database when saving their parent.

TransientPropertyValueException: object references an unsaved transient instance - save the transient instance beforeQuery flushing

Since you are not cascading coupon then you need it to be managed before saving CouponHistory, luckly when saving an entity save() will return the managed persisted entity so all you need is to assign it to coupon

 @Transactional
public void createCoupon() {
Coupon coupon = new Coupon();
coupon.setCode(RandomStringUtils.randomAlphanumeric(5));
coupon.setValidity(1);
coupon = couponRepository.save(coupon);//save will return the managed entity

CouponHistory couponHistory = new CouponHistory();
couponHistory.setCreatedOn(new Date());
couponHistory.setCoupon(coupon);
couponHistoryRepository.save(couponHistory);
}

TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing :

just simply soled the issue

{
"allOrgMstSection": {
"id": 23
},
"allOrgMstSubSection": null,
}

Why I get Object references an unsaved transient instance

You have cascade = {CascadeType.ALL } on your Tag class but not on your Ads class, if you want to save your Ads object and cascade save the Tag objects in it, then you need the annotation on the field of your Ads class. Otherwise save the Tag object instead of the Ads object.



Related Topics



Leave a reply



Submit