-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Postgres Annotations Cascade One-To-Many
PostPosted: Wed Jan 24, 2007 5:03 pm 
Newbie

Joined: Thu Dec 02, 2004 2:13 pm
Posts: 10
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.