Skip to main content

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

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
-- 
View this message in context: http://www.nabble.com/Foreign-key-relationships-not-quite-working-tp22103998p22103998.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top