-->
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: Newbie problem with One-to-Many relationship with annotation
PostPosted: Mon Nov 02, 2009 3:31 pm 
Newbie

Joined: Mon Nov 02, 2009 3:27 pm
Posts: 5
Hi,

I'm having trouble using hibernate with JPA annotations. I'm a newbie so apologies if this is a stupid question.

I'm trying to do one-to-many relationship between a Product entity and Version entities. A product has many versions, each of which has one product. Code is shown below.

I want a deletion of the product to cascade through to the Version table and remove all versions associated to that product.

The PostgreSQL error I'm getting is

"Caused by: org.postgresql.util.PSQLException: ERROR: update or delete on table "product" violates foreign key constraint "fk782db4b819a92fb2" on table "version"
Detail: Key (id)=(2) is still referenced from table "version"."

which leads to:
"Caused by: org.hibernate.exception.ConstraintViolationException: could not execute update query"


Even though I specify CascadeType.ALL (or REMOVE) in my annotation, the version table ends up with the following foreign key constraint:

REFERENCES product () MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION;


----------------------------------------------------------------------------
Code:
/* skipping imports and package*/

@Entity
public class Product {

    public static final String DELETE_allProducts = "DELETE FROM Product";

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Basic
    @Column(nullable = false, length = 40)
    private String name;

    @OneToMany(mappedBy="product", cascade=CascadeType.ALL)
    List<Version> versions;

}

/* skipping imports and package*/

@Entity
  public class Version {
   
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   long id;
   
   @Basic
    @Column(nullable = false, length = 30)
    String name;

    @ManyToOne
    Product product;

}


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.