Hibernate 级联保存失败,Hibernate defect

使用annotation方式标注Payment和PaymentEntry. ID都是用自己设定策略assigned。
@Entity
@Table(name = "LO_CONF_PAYMENT")
public class PaymentBillDO implements Serializable{
	

	@Transient
	private static final long serialVersionUID = 206001219164196538L;
	
	
	@Id
	@Column(name = "ID", length = 50)
	@GeneratedValue(generator = "c-assigned")
	@GenericGenerator(name = "c-assigned", strategy = "assigned")
	private String ID;

 	@OneToMany(mappedBy ="parent",
			targetEntity = PaymentBillEntryDO.class, fetch = FetchType.LAZY)
	@Cascade({ CascadeType.ALL})
	private List<PaymentBillEntryDO> Entries;
}


@Entity
@Table(name = "LO_CONF_PAYMENTENTRY")
public class PaymentBillEntryDO implements Serializable {

	private static final long serialVersionUID = 6913338048891980882L;

	@Id
	@Column(name = "ID", length = 50)
	@GeneratedValue(generator = "c-assigned")
	@GenericGenerator(name = "c-assigned", strategy = "assigned")	
	private Long ID;
	
	@ManyToOne( targetEntity = PaymentBillDO.class, fetch = FetchType.EAGER)
	@JoinColumn(name = "PARENT_ID", nullable = false)
	@Cascade({ CascadeType.REFRESH })
	private PaymentBillDO  parent;


无论怎么测试,都不能级联保存。
搜索了无数网页,中文的,英文都,都没有解。搜到过一点点有用的资料,就是把@Cascade有JPA的annotaion换成Hibernate的annotaion。其他的各种sample都只是对hibernate reference的抄袭而已。
于是只能debug hibernate。在类DelegatingStatement的executeBatch方法中居然发现了对entry的cascade update语句,而不是想象中的Insert语句。想了一下,难道hibernate如果发现entry 有ID的话,就判断对entry进行级联update,而不是进行insert。 后来把ID生成策略换成了SEQUENCE,解决问题。

崩溃啊。
新的Entry的annotation为

@Entity
@Table(name = "LO_CONF_PAYMENTENTRY")
public class PaymentBillEntryDO implements Serializable {

	private static final long serialVersionUID = 6913338048891980882L;

	@Id
	@Column(name = "ID", length = 50)
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "paymentbillEntry_seq")
      @SequenceGenerator(name = "paymentbillEntry_seq", sequenceName = "SEQ_LO_CONF_PAYMENT_ENTRY")	
	private Long ID;
	
	@ManyToOne( targetEntity = ElementEventRelationshipDO.class, fetch = FetchType.EAGER)
	@JoinColumn(name = "PARENT_ID", nullable = false)
	@Cascade({ CascadeType.REFRESH })
	private PaymentBillDO  parent;


  希望大家出现这个问题的时候,搜到的答案不仅仅是copy reference而已。

你可能感兴趣的:(C++,c,Hibernate,C#,jpa)