TransientPropertyValueException:对象引用了一个未保存的瞬态实例-在查询刷新之前保存该瞬态实例 [英] TransientPropertyValueException: object references an unsaved transient instance - save the transient instance beforeQuery flushing

查看:83
本文介绍了TransientPropertyValueException:对象引用了一个未保存的瞬态实例-在查询刷新之前保存该瞬态实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个冬眠实体Coupon和CouponHistory,并且CouponHistory和Coupon之间具有单向关系.

I have two hibernate entities Coupon and CouponHistory with Uni Directional Relationship between CouponHistory and Coupon.

@Entity
@Table(name = "validity_coupon")
public class Coupon {

@Id
@Column(length = 50, unique = true, nullable = false)
private String code;

private int validity;
private boolean used;

...}


@Entity
@Table(name = "coupon_history")
@TableGenerator(name = "seqGen", table = "shunya_id_gen", pkColumnName = "GEN_KEY", valueColumnName = "GEN_VALUE",
    pkColumnValue = "coupon_history_seq", allocationSize = 1)
public class CouponHistory {

@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "seqGen")
private long id;

@Temporal(TemporalType.TIMESTAMP)
private Date createdOn;

@ManyToOne(fetch = FetchType.LAZY)
private Coupon coupon;

...}

有一种事务服务方法,该方法试图将两个实体都保存在单个事务中.在这里,Spring被用来处理事务.

There is a Transactional Service Method that tries to saves both the entities in a Single Transaction. Spring is being used to handle transaction here.

@Transactional
public void createCoupon() {
    Coupon coupon = new Coupon();
    coupon.setCode(RandomStringUtils.randomAlphanumeric(5));
    coupon.setValidity(1);
    couponRepository.save(coupon);

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

调用此方法时出现以下异常-

I get the below exception when i call this method -

org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance beforeQuery flushing : com.poc.CouponHistory.validityCoupon -> com.poc.Coupon; nested exception is java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance beforeQuery flushing : com.poc.CouponHistory.validityCoupon -> com.poc.Coupon

当我在单个交易中将子实体保存在父实体之前时,我不明白为什么冬眠会在抱怨我.

I don't understand why hibernate is complaining me when i have saved the child entity before parent entity in single transaction.

如果我将优惠券实体的ID生成更改为自动",则一切开始正常运行.但是我想手动分配优惠券代码,因此ID自动生成不在范围内.

If i change id generation to Auto for Coupon Entity, then everything starts working fine. But i want to manually assign the Coupon Code so Id auto generation is not in scope.

任何帮助将不胜感激!

推荐答案

由于您不是级联优惠券,因此需要在保存CouponHistory之前对其进行管理,幸运的是,在保存实体时save()将返回托管的持久化实体,因此所有您需要将其分配给优惠券

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:对象引用了一个未保存的瞬态实例-在查询刷新之前保存该瞬态实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆