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

Why specify man-to-one, one-to-many etc in Hibernate ?

 
James Clarke
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

what is the purpose of specifying man-to-one, one-to-many etc mappings in Hibernate ??

Apart from the compiler being able to verify that it has the right return type declared, I dont see any benefit in this.

Thanks,

J.C
 
Javid Jamae
Author
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using mapping files, it doesn't default to mapping every attribute of a class the same way it does with annotations. So you have to explicitly tell it what you want mapped. This argument carries for many-to-one at least. Also, with many-to-one, you will often specify attributes such as:



But with default, and assuming the column attribute is implied to point to the id defined on the "one" side, you would just have the name attribute.

With one-to-many, I'm not sure. You are inside of the definition for a collection already, and it should be able to infer the type of the object.

I'm not quite sure of what the argument is for why you are required to specify @OneToMany and @ManyToOne when using annotations.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic