Meaningless Drivel is fun!*
The moose likes Object Relational Mapping and the fly likes UniqueConstraint on foreign key (ManyToOne). Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "UniqueConstraint on foreign key (ManyToOne)." Watch "UniqueConstraint on foreign key (ManyToOne)." New topic

UniqueConstraint on foreign key (ManyToOne).

Gasan Guseynov
Ranch Hand

Joined: Jan 03, 2006
Posts: 67
Hi, guys.

Searched for the answer, but didn't find.

Here is what I have:

I think you inferred what is the problem. Yes, constraints aren't created. Just messed up with this.
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
What error are you getting?

You Entity does not have an @Id, so is not valid. You seem to want both ManyToOne foreign keys to be the Id?

In JPA 2.0 you can just add @Id to both.

In JPA 1.0, it is more complex, see,

You object also seems to just be a many-to-many join table, if you mapped it as a ManyToMany, you would not need a class for it at all.

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
Gasan Guseynov
Ranch Hand

Joined: Jan 03, 2006
Posts: 67
Hi James.

Thanks for your answer it seems meaningful.

I'm sorry, I just forgot the @Id property, heh. So I have it. I want that 2 to be not ID, but simply unique. Unique constraint on (apple_id, watermelon_id) that are foreign keys. Stupid isn't it? But, for my misery, this is what I want.
Gasan Guseynov
Ranch Hand

Joined: Jan 03, 2006
Posts: 67
By the way, thanks for the link. JPA 1.0 or 2.0 Just don't know, I always thought that JPA is JPA without versions. By the way, I'm using Hibernate, so maybe it doesn't matter what JPA version it is. It's simple annotations isn't it?
I agree. Here's the link:
subject: UniqueConstraint on foreign key (ManyToOne).
Similar Threads
failed to lazily initialize a collection of role
How to do s:if test of a value in the iterator?
Access Modifier protected -- SCJP Study Guide page 36
Protected method in a packaged superclass not being called by subclass