1

I have the following classes

  1. 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;
    }
    
  2. 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;
     }
    
  3. 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?

Harihar Das
  • 484
  • 3
  • 11

1 Answers1

0

In @Inheritance(strategy= InheritanceType.JOINED) you have to define a @DiscriminatorColumn to determine the class of the row. Adding this should resolve the problem. If you are using Hibernate 3.1 or lower (hopefully you're not), @DiscriminatorFormula should do the job, but I never really tried it personally.

Next on your AddedItem entity, the @PrimaryKeyJoinColumn(name = "NAME") (javadoc, stackoverflow is pretty much just for show, but it should not cause any harm.

If AddedItem is public, so should Item entity be.

edit: Since your ID field is a String, are you sure you are using correct unique values for it?

Community
  • 1
  • 1
awb
  • 324
  • 2
  • 9