I'm having a problem persisting the following a self referencing class to a postgres 8.1 database.
Annotated Class:
Code:
@Entity
@Table(name = "sales_order_detail", schema = "public", uniqueConstraints = {})
public class OrderItem implements Serializable {
private static Logger log = Logger.getLogger(OrderItem.class);
private static final long serialVersionUID = -2033469067612466530L;
// Fields
@Id
@SequenceGenerator(name = "order_detail_sequence", sequenceName = "sales_order_detail_id_seq")
@GeneratedValue (strategy = GenerationType.SEQUENCE, generator = "order_detail_sequence")
@Column(name = "id")
private Integer id = null;
@ManyToOne(cascade= {CascadeType.ALL})
@JoinColumn(name="parent_id", insertable=true, updatable=false)
private OrderItem parent;
@OneToMany(mappedBy="parent")
private Set<OrderItem> children = new HashSet<OrderItem>();
@ManyToOne
@JoinColumn(name="order_id", insertable=true, updatable=false)
private Order order;
@Column(name = "item_id")
private int itemId;
When I perform the following actions:
Code:
Product p = (Product)this.dao.getObject(Product.class, Integer.valueOf(223));
OrderItem parent = new OrderItem(p, 1);
p = (Product)this.dao.getObject(Product.class, Integer.valueOf(1));
OrderItem child = new OrderItem(p, 2);
parent.addChild(child);
// child adds reference to parent in the add child method.
parent.setOrder(order);
this.dao.saveObject(parent);
hibernateTemplate.flush();
I get the following exception:
Hibernate: select nextval ('iq2_sales_order_detail_id_seq')
Hibernate: insert into public.iq2_sales_order_detail (parent_id, order_id, product_id, size_id, quantity, price, item_id, version, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
13:54:51,218:WARN [main] JDBCExceptionReporter.logExceptions | SQL Error: 0, SQLState: 23505
13:54:51,218:ERROR [main] JDBCExceptionReporter.logExceptions | ERROR: duplicate key violates unique constraint "iq2_sales_order_detail_order_id_key"
13:54:51,218:ERROR [main] AbstractFlushingEventListener.performExecutions | Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: could not insert: [com.pizza73.model.OrderItem]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2202)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2595)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:51)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: org.postgresql.util.PSQLException: ERROR: duplicate key violates unique constraint "iq2_sales_order_detail_order_id_key"
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1531)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1313)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:188)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:452)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:354)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:308)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2182)
... 31 more
The id of the table is a serial data type.
Thanks,
Chris.