【发布时间】:2014-06-29 00:20:33
【问题描述】:
我有以下课程
-
具体类Item
@Entity @Table(name = "ITEM") @Inheritance(strategy= InheritanceType.JOINED) Class Item implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name = "NAME") private final String name; @Column(name = "PRICE") private BigDecimal price; } -
扩展Item的具体类AddedItem
@Entity @Table(name = "ADDED_ITEM") @PrimaryKeyJoinColumn(name = "NAME") public class AddedItem extends Item { @Column(name = "TOTAL_PRICE") private BigDecimal totalPrice; @Column(name = "QUANTITY") private Integer quantity; @ManyToOne @JoinColumn(name = "INVOICE_ID") private Invoice invoice; } -
Invoice 类与 AdditionalItem 具有一对多映射
@Entity @Table(name = "INVOICE" public class Invoice implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "INVOICE_ID") private Long billNo; @Column(name = "BILLDATE") @Temporal(TemporalType.TIMESTAMP) private Date billDate; @Column(name = "CUSTOMERNAME") private String customerName; @OneToMany(fetch = FetchType.LAZY, mappedBy = "invoice", cascade = CascadeType.ALL) private final List<AddedItem> itemsAdded; }
当我尝试保存 Invoice 实例时,出现以下异常:
org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: ORA-00001: unique constraint (ITEM_PK) violated
org.hibernate.event.def.AbstractFlushingEventListener performExecutions
SEVERE: Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
...
当我预取 Item 相关信息时,如何告诉 Hibernate 只存储子类信息?
【问题讨论】:
标签: hibernate persistence