Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Foreign key relationships not quite working

Make sure you set both sides of the relationship when adding objects to the
collection.

See,

http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Object_corruption.2C_one_side_of_the_relationship_is_not_updated_after_updating_the_other_side


jxt wrote:
> 
> Hi. I'm only partially familiar with JPA and EclipseLink, and I'm having a
> problem getting my foreign key relationships working as expected.
> 
> I'm letting EclipseLink, and/or JPA, and/or NetBeans generate my
> database tables from entity classes. Actually, most of the entity
> classes were first generated going the other way -- from the
> database tables themselves. But that wasn't quite working, so I
> took the generated entity classes and hacked on them a little to
> get things (mostly) working and then set the option in the persistence.xml
> file to drop and create the tables for me, which works better.
> 
> However, my one-to-many relationships specified in the entity classes
> don't seem to quite make it into the generated database tables. An example
> is, in the "parent" entity class "Entries":
> 
> @OneToMany (mappedBy = "entryId", cascade={CascadeType.ALL})
> private List<EntryTags> entryTagsCollection;
> 
> and then in the "child" entity class, "EntryTags":
> 
> @JoinColumn(name="entry_id", referencedColumnName = "id")
> @ManyToOne
> private Entries entryId;
> 
> where the parent class, Entries, has id as the primary key.
> 
> This setup MOSTLY works. The generated tables (postgresql) appear to
> have the correct foreign key relationships created, but when I populate
> the parent and child tables the "entry_id" column in the EntryTags table
> never gets populated with the id of the Entries row to which it (should)
> refer. And I can (manually, with psql) delete the Entries row without
> affecting the EntryTags rows.
> 
> I've tried adding cascade={CascadeType.ALL} to the @ManyToOne annotation
> without changing the behavior. There must be something small that I'm
> missing. Here is the EntryTags table as shown by psql:
> 
> 
>           Table "public.entrytags"
>   Column  |          Type          | Modifiers
> ----------+------------------------+-----------
>  id       | integer                | not null
>  term     | character varying(255) |
>  via      | character varying(255) |
>  entry_id | integer                |
> Indexes:
>     "entrytags_pkey" PRIMARY KEY, btree (id)
> Foreign-key constraints:
>     "fk_entrytags_entry_id" FOREIGN KEY (entry_id) REFERENCES
> entries(id)
> 
> Looks okay to me. Any ideas on what's wrong?
> 
> Thanks,
> Johnny Tolliver
> 


-----
---
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland 
http://www.eclipse.org/eclipselink/
 EclipseLink ,  http://www.oracle.com/technology/products/ias/toplink/
TopLink 
Wiki:  http://wiki.eclipse.org/EclipseLink EclipseLink , 
http://wiki.oracle.com/page/TopLink TopLink 
Forums:  http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink , 
http://www.nabble.com/EclipseLink-f26430.html EclipseLink 
Book:  http://en.wikibooks.org/wiki/Java_Persistence Java Persistence 
-- 
View this message in context: http://www.nabble.com/Foreign-key-relationships-not-quite-working-tp22103998p22161044.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top