This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes JPA (TopLink) @ManyToMany generates strange tables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "JPA (TopLink) @ManyToMany generates strange tables" Watch "JPA (TopLink) @ManyToMany generates strange tables" New topic
Author

JPA (TopLink) @ManyToMany generates strange tables

Morten Franorge
Ranch Hand

Joined: Jul 29, 2005
Posts: 137
Hi,

I'm setting up a @ManyToMany relation in my object model, but I'm having strange results when Toplink generates the join table for me.






When using this mapping, TopLink Generates the following SQL:


The problem is the primary key. It uses employee_id as PK, however this contraint would restrict the object model. Only allowing the employee to have one project.

I've also tried to set a uniqueConstraint in the @JoinTable annotation, like this:

The result was the same SQL.

When I removed the @JoinTable annotation, the following SQL was created:

This is slightly better, but this allows multiple rows setting up the same project-employee relation.

In a production enviroment I would of course not use the table generation, however the way that Toplink generates the tables makes testing more time consuming.

Any one who knows what I'm doing wrong? Or is it TopLink's fault?
[ November 07, 2006: Message edited by: Morten Fra Norge ]

SCJP 1.4, SCBCD 1.3, SCBCD 5.0, SCEA J2EE, SCEA 5.0
Mike Keith
author
Ranch Hand

Joined: Jul 14, 2005
Posts: 304
Hi Morten,

I tried doing what you did and the table got generated correctly with the compound PK containing both fields when I used the UniqueConstraint element. I might have a newer version of TopLink Essentials than you though since I just recently got a fresh version.

-Mike


-Mike
Pro JPA 2: Mastering the Java Persistence API
Morten Franorge
Ranch Hand

Joined: Jul 29, 2005
Posts: 137
Originally posted by Mike Keith:
Hi Morten,

I tried doing what you did and the table got generated correctly with the compound PK containing both fields when I used the UniqueConstraint element. I might have a newer version of TopLink Essentials than you though since I just recently got a fresh version.

-Mike


Which db did you use? I tried using HSQL. I finally got it working now though, not sure what changed the generated code. Might be a problem with my build tools...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JPA (TopLink) @ManyToMany generates strange tables
 
Similar Threads
Hibernate Annotation - ManyToMany within same PK on an external table
Toplink refresh ManyToMany
Hibernate: many-to-many --how to add a constrain?
Confused with many to many,
Integrate multiple JPA projects