有两个实体,
例如,值:1个活动,2个非活动等)
例如: 10个橙子1,20个苹果2等)
试图弄清楚要使用什么关系以及如何使用。我尝试了OneToOne、OneToMany、ManyToOne、ManyToMany等,但每次状态实体都会添加一个新行
例如,21个活动,22个活动,23个活动,等等。
优惠券实体应在每次保存新优惠券时获得新行,并使用状态id字段中的状态主键,但状态实体应获得添加的新行。
Yes, Many coupons can share the same status.
The following code produces the following results
Before execution
Status Table
StatusId STATUS
1 ACTIVE
2 INACTIVE
Coupon Table
CouponId Title STATUS
<no rows at present>
After execution
Status Table
StatusId STATUS
1 ACTIVE
2 INACTIVE
3 ACTIVE (This row should not get inserted)
Coupon Table
CouponId Title StatusId
10 Apples 3 (StatusId should be 1 instead of 3)
JSON Request:
{
"title": "Apples",
"status": "ACTIVE",
}
Code:
@Autowired
CouponRespository couponRepository;
@RequestMapping(value = "/coupon", method = RequestMethod.POST)
void createCoupon(@RequestBody Coupon coupon) {
couponRepository.save(coupon);
}
public interface CouponRespository extends JpaRepository<Coupon, Long> {
}
@Entity
public class Status implements Serializable {
@Id
@SequenceGenerator(name = "STATUS_STATUSID_GENERATOR", sequenceName = "STATUS_ID_SEQ")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "STATUS_STATUSID_GENERATOR")
@Column(name = "STATUS_ID")
private long statusId;
@Column(name = "STATUS")
private String status;
}
@Entity
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "couponId")
@JsonIgnoreProperties({ "statusId" })
public class Coupon implements Serializable {
@Id
@SequenceGenerator(name = "COUPON_COUPONID_GENERATOR", sequenceName = "COUPON_ID_SEQ")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "COUPON_COUPONID_GENERATOR")
@Column(name = "COUPON_ID")
private long couponId;
@Column(name = "TITLE")
private String title;
@Column(name = "STATUS_ID")
private Long statusId;
// Need help here how to set this up properly
@ManyToOne(cascade = { CascadeType.ALL })
@JoinColumn(name = "STATUS_ID", insertable = false, updatable = false)
private Status status;
}任何帮助都是非常感谢的。
发布于 2016-07-10 01:46:51
我不知道Jackson如何创建一个包含您问题中JSON的Status实例的Coupon实例,更不知道新行是如何出现在数据库中的,因为COupon的status字段是不可插入的,但不管怎样,假设它做了以下等效项:
Coupon coupon = new Coupon();
coupon.setTitle("Apples");
Status status = new Status("ACTIVE");
coupon.setStatus(status);您要求将优惠券保存到JPA。并且您告诉JPA将持久化操作级联到优惠券中包含的状态。状态没有任何ID,那么除了创建一个新的ID之外,JPA还能做什么呢?它怎么能猜到您实际上想要的是将新的优惠券与ID 1所标识的现有状态相关联?它不能。它会按你说的做。
那么,你应该怎么做呢?
首先,从Coupon中删除statusId字段。这是状态的ID,您在优惠券中有状态,因此此信息是多余的。
其次,您希望优惠券与现有的状态相关联。因此,获取对此现有状态的引用,并在保存优惠券之前将其设置在优惠券中:
// this should execute a query that retrieves the Status entity
// that has the "ACTIVE" status
Status status = statusRepository.findStatusByStatus("ACTIVE");
coupon.setStatus(status);
couponRepository.save(coupon);第三:由于您不希望数据库中的两个状态具有相同的状态,因此应为表添加一个唯一约束。
第四:由于您不希望在保存、修改或删除引用此状态的优惠券时保存、修改或删除此状态,请删除cascade = { CascadeType.ALL }。
第五:因为您希望对coupon.status的更改是持久的,所以删除insertable = false, updatable = false。
第六:由于您在控制器中接收的JSON不具有优惠券的结构,并且不是优惠券实例,而是标题和状态文本,允许您使用该文本查找状态并创建优惠券实例,因此创建一个与您的JSON具有相同结构的类CouponCommandDTO,并使其成为控制器的参数类型。然后使用上面显示的代码将此DTO转换为实际的优惠券。
https://stackoverflow.com/questions/38284104
复制相似问题