0

In jpa I have some entity (Content and Product for now but more can come) that should join to Comment entity.

Problem is I don't want to have extra field(and column in table) in Comment entity for each join entity (Products, Contents..) because these entities will increase in future.

I find one semi-solution is to use single table inheritance and create concrete Comment class like CommentContent, CommentProduct, and use discriminator column but joining to entities (Content and Product) still remain.

what do you suggest?

Edit:

sample relation for example between Comment and Content will be @MayToOne that many Comments belongs to one Content and so for Product..

Edit 2

in pure table relationship schema (without ORM like hibernate/jpa) I can and I do this kind of solution: add to column in comment table 1-item_type and 2-item_id witch item_type specify other side table name (product, content in my question) and item_id specify foreign key to table that it name is at item_type column

How can I model this in Jpa/hibernate ORM?

mohsenJsh
  • 2,048
  • 3
  • 25
  • 49
  • Can you clarify if Product entity have any relation with Content entity or not ? this would help us so help you too :) – mibrahim.iti Feb 28 '17 at 00:16
  • Also you should know there are more than one inheritance strategy, please check https://dzone.com/articles/jpa-implementation-patterns-mapping and http://www.datanucleus.org/products/datanucleus/jpa/orm/inheritance.html – mibrahim.iti Feb 28 '17 at 00:20
  • @mibrahim.iti there isn't any relation between product and content, but both of them have relation to comment, problem is i don't want seperate column in comment table to relate comment to entities – mohsenJsh Feb 28 '17 at 05:04

3 Answers3

1

You can model what you described with Hibernate like

class Content {
  @OneToMany
  @JoinColum(name = "item_id")
  @Where("item_type = 'CONTENT'")
  Set<Comment> comments;
}

class Product {
  @OneToMany
  @JoinColum(name = "item_id")
  @Where("item_type = 'PRODUCT'")
  Set<Comment> comments;
}

class Comment {
  @Id
  @GeneratedValue
  Long id;
  @Enumerated(STRING)
  ItemType itemType;
  Lont itemId;
}

enum ItemType {
  CONTENT,
  PRODUCT
}
Christian Beikov
  • 15,141
  • 2
  • 32
  • 58
0

What type or relation is this?

If it is @OneToOne you can put information about relation with Comment in Product and Content, and in Comment use option MappedBy to map them, you will skip the extra columns in Comment.

If it is @OneToMany you won't make any column in Comment but rather again put information in Product and Content and only use mappedBy on collection of this objects on the Comment site.

CheckForMoreInfo

Community
  • 1
  • 1
Emil Hotkowski
  • 2,233
  • 1
  • 13
  • 19
0

You can try @ManyToOne for your upcoming entities. So here you don't need to add new columns in Comment entity(base table) Check this example : https://howtoprogramwithjava.com/hibernate-manytoone-unidirectional-tutorial/

Beginner
  • 855
  • 9
  • 21
  • 37