A friendly place for Java greenhorns!
Java
Ranch
»
Big Moose Saloon
Search
|
FAQ
|
Recent Topics
|
Hot Topics
Register
/
Login
Java
Ranch
»
Forums
»
Java
»
Object Relational Mapping
Author
CascadeType.ALL is not doing what I expected (int constraint violated)
Logan Lee
Greenhorn
Joined: Oct 28, 2009
Messages: 10
posted
Feb 04, 2010 06:58:57
I am using JBoss5.1.0GA (hibernate) and Oracle XE.
A book can have one or multiple authors (as reflected below)
When I persist a book, it contains a collection of authors. I want the authors also to be persisted (cascade)
I have three tables.
BOOK_TBL
BOOK_ID (PK)
BOOK_AUTHOR_TBL
BOOK_ID (FK)
AUTHOR_ID (FK)
AUTHOR_TBL
AUTHOR_ID (PK)
I have two Entities:
Code snippets:
Book
@Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "BOOK_ID_SEQ") @SequenceGenerator(name = "BOOK_ID_SEQ", sequenceName = "BOOK_TBL_SEQ", allocationSize = 1) @Column(name="BOOK_ID") public Long getBookId() { return bookId; }
@OneToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER) @JoinTable(name = "BOOK_AUTHOR_TBL", joinColumns = @JoinColumn(name = "BOOK_ID"), inverseJoinColumns = @JoinColumn(name = "AUTHOR_ID")) public List<Author> getAuthors() { return authors; }
Author
Nothing special here, except I use a sequence for the PK
@Id @SequenceGenerator(name = "AUTHOR_ID_SEQ", sequenceName = "AUTHOR_TBL_SEQ", allocationSize = 1) @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="AUTHOR_ID_SEQ") @Column(name="AUTHOR_ID") public Long getAuthorId() { return authorId; }
When I persist my book, I get:
Exception in thread "main" javax.ejb.EJBTransactionRolledbackException: Transaction rolled back ... Caused by: java.sql.BatchUpdateException: ORA-02291: integrity constraint (schema.B OOK_AUTHOR_AUTHOR_ID) violated - parent key not found
So it seems it's trying to insert the FK's in the mapping table but can't find the parent key yet.. What am I doing wrong?
This message was edited 3 times. Last update was at
Feb 04, 2010 07:02:39
by Logan Lee
Logan Lee
Greenhorn
Joined: Oct 28, 2009
Messages: 10
posted
Feb 09, 2010 01:51:52
any ideas? all help is very much appreciated
Java
Ranch
»
Forums
»
Java
»
Object Relational Mapping
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter