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
How to Call One Constructor from Another in Java
Java 8 Lambda Function That Throws Exception
Jtextfields on Top of Active Drawing on Jpanel, Threading Problems
How to Convert from Int to String
What Is the Concept of Erasure in Generics in Java
Error: the Processing Instruction Target Matching "[Xx][Mm][Ll]" Is Not Allowed
How to Update a Jfreechart'S Appearance After It's Been Made Visible
Initialization of an Arraylist in One Line
How to Initialize an Array in Java
Arrays.Aslist() Not Working as It Should
How to Add an Image to a Jpanel
What's Wrong With Using == to Compare Floats in Java
Y Returns 2012 While Y Returns 2011 in Simpledateformat
How to Check If a File Exists in Java
How Many Characters Can a Java String Have