Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

UniqueConstraint on foreign key (ManyToOne).  RSS feed

 
Gasan Guseynov
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
http://en.wikibooks.org/wiki/Java_Persistence/ManyToMany#Mapping_a_Join_Table_with_Additional_Columns

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.
 
Gasan Guseynov
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!