I have the following classes
Concrete class 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; }
Concrete class AddedItem that extends Item
@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; }
The Invoice class that has a one to many mapping with AddedItem
@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; }
When I try to save an Invoice instance I am getting the following exception:
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
...
How can I tell Hibernate to store only the subclass information as I prefetch the Item related information?