简体   繁体   English

JPA加入继承的类

[英]JPA join to inherited class

In jpa I have some entity ( Content and Product for now but more can come) that should join to Comment entity. 在jpa中,我有一些实体(现在有ContentProduct ,但将来还会有更多),应该加入Comment实体。

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. 问题是我不想在Comment实体中为每个联接实体(Products,Contents ..)增加多余的字段(表中的列),因为这些实体将来会增加。

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. 我发现一个半解决方案是使用单表继承和创造具体的评论类像CommentContentCommentProduct ,并使用鉴别列,但加盟的实体(内容和产品)仍然存在。

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.. 例如,评论和内容之间的示例关系将是@MayToOne ,其中许多评论属于一个内容,因此对于Product。

Edit 2 编辑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 在纯表关系模式(没有像hibernate / jpa这样的ORM)中,我可以做这种解决方案:将其添加到注释表1-item_type2-item_id中的item巫婆item_type指定其他边表名称(产品,我的问题内容) )和item_id指定表名称为item_type列的表的外键

How can I model this in Jpa/hibernate ORM? 如何在Jpa /休眠ORM中对此建模?

You can model what you described with Hibernate like 您可以像使用Hibernate描述的那样建模

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
}

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. 如果是@OneToOne ,则可以在“产品和内容”中放置有关与Comment的关系的信息,在Comment使用选项MappedBy进行映射时,您将跳过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. 如果它是@OneToMany ,则不会在Comment中创建任何列,而是将信息再次放置在Product和Content中,并且仅在Comment网站上对该对象的集合使用maptedBy。

CheckForMoreInfo CheckForMoreInfo

You can try @ManyToOne for your upcoming entities. 您可以为即将到来的实体尝试@ManyToOne。 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/ 因此,这里您不需要在Comment实体(基本表)中添加新列。请检查以下示例: https : //howtoprogramwithjava.com/hibernate-manytoone-unidirectional-tutorial/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM