Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to create a identified 1xN relation using Hibernate?

 
William Fernandes
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

How do I create this relation using Hibernate with Annotations?




Some code that might help:



I tried almost everything (@Id, @Embeddable, @ManyToOne,etc.) nothing seems to work.
Thank you!
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll want to add setters and getters for the Edition in the Book class.

This looks like a one to one relationship. I have an example that is very similar using an Exam and ExamDetail

Sample One to One Association using JPA Annotations

You'll need to add a @OneToOne annotation at some point. It looks something like this.



Let us know how it works out!

 
William Fernandes
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cameron Wallace McKenzie wrote:You'll want to add setters and getters for the Edition in the Book class.

This looks like a one to one relationship. I have an example that is very similar using an Exam and ExamDetail

Sample One to One Association using JPA Annotations

You'll need to add a @OneToOne annotation at some point. It looks something like this.



Let us know how it works out!


Hi Cameron,

I tried @OneToOne, but that just add an FK in the Edition table, I'd like that FK to be PK in the table also.

 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then you'd probably need a one to many. A book can have many editions, or an edition can be associated with many books?

I'd figure a book can only be associated with one addition?
 
William Fernandes
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cameron Wallace McKenzie wrote:Then you'd probably need a one to many. A book can have many editions, or an edition can be associated with many books?

I'd figure a book can only be associated with one addition?


Yes a book can have many editions (Book 1 can have edition 1, 2,3,4...)
Anyway that was just an example that I created to try to get this kind of table, it's not very logical anyway.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're just hacking around, learning the various mappings, check out some of the examples on my site.

Similar to your 'book has editions' I have an example of 'a team has players:



The various mappings for this example are here:

http://jpa.ezhibernate.com/Javacode/learn.jsp?tutorial=18mappingonetomanyassociations





One To Many Mappings Tutorial for Hibernate

-Cameron McKenzie



 
William Fernandes
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Cameron, but I really wanted to "translate" to Hibernate this identified relation, where the FK is also PK.
I already know many relations, but this one in particular seems very hard, I thought it was simple, because it was so common in databases.

PS:Are you the author of that book Hibernate Made Easy?
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, that little book is mine.

You might also want to look at embedded ids, which is really what you have there in the edition - the composite key.

-Cameron McKenzie (Author of Hibernate Made Easy)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic