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

many-to-many link table

 
Linwood Hayes
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read from internet that ,  for many-to-many table relation, if the link table is a pure link table (i.e. only has the IDs from each of the two tables), then we don't need to create a POJO entity for it.  (see http://www.mkyong.com/hibernate/hibernate-many-to-many-relationship-example/) .  But if the link table includes some extra business fields then we need a separate POJO entity for it (see https://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/)

This makes good sense.  Now, if the link table consist of its own surrogate key  in addition to the two FK ids from two tables, do we need to create POJO for the link table ? i.e. if the link table looks like



should we consider stock_category_id  as extra business field  to create a POJO for this table ?
 
Stephan van Hulst
Bartender
Pie
Posts: 6503
83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The need to create any class at all is based purely on what you're going to use it for. You don't have to create a class if you're not interested in the identity of the relation. You don't have to create a class if you're not interested in any other business fields that may be associated with the relation.

So the question is: why do you need the relation to have a separate identifier, if a relation can uniquely be identified by a compound key?
 
Linwood Hayes
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:The need to create any class at all is based purely on what you're going to use it for. You don't have to create a class if you're not interested in the identity of the relation. You don't have to create a class if you're not interested in any other business fields that may be associated with the relation.

So the question is: why do you need the relation to have a separate identifier, if a relation can uniquely be identified by a compound key?


My understanding is :  If the link table consists of two fields (one is the PK from STOCK table and the other is the PK from CATEGORY table), then we don't need a separate class for this table.   But if the link table also has its own PK like some unique auto generated id, do we need a separate class to map to it ?  I guess we don't  but I am not sure and that's why I was asking.  Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic