@MappedSuperClass上的JPA查询。获取所有子类的详细信息?

时间:2017-03-30 12:10:10

标签: hibernate jpa spring-boot spring-hateoas

我有3个单选按钮 1.Car 2.Bike 3.Both 。所以,如果我选择  汽车它将获取所有的汽车细节,如果我选择2只会  获取汽车细节,直到这里我能够实现,但如何  如果我选择第3个单选按钮,则同时获取Car和bike详细信息  “都”。在下面的例子中,我想在选择“both”时也这样做         它将获取所有文件。对此最好的解决方案是什么?

    Parent class:
    @MappedSuperclass
    public abstract class BaseProsecutionDocument {

    private long dmsDocumentId;
    private long documentVersion;
    private String fileName;
    …
    }

    Pros class:

    @Entity
    @Table(schema = “reference”, name = “prosecution_documents”)
    public class ProsDocument extends BaseProsecutionDocument {

    private Long id;
    private Long prosId;
    private Long ocportalSubmissionId;
    …
    }

    Sumisiion class:

    @Entity
    @Immutable
    @Table(schema = “reference”, name = “submission_docs”)
    public class submissionDocument extends BaseProsecutionDocument {

    private Long id;
    private Long inventionId;
    …
    }
    I want to know how to write the query for that..like
    i have written for those 2 radio buttons:

    public interface ProsecutionDocumentRepository extends JpaRepository {
    @Query(value = “SELECT ppd FROM ProsDocument ppd ” +
    “WHERE ppd.submissionId IN (SELECT p.id FROM submission p WHERE 
UPPER(p.doc) = UPPER(:doc)) ” +
    “AND ppd.documentType.documentType in (‘OFFICE’)”)
    Page findSubmissionOfficeDocumentsByDoc(@Param(“doc”) String docket, 
Pageable pageable);
    }
  

或者我是否需要将@MappedSuperClass更改为@Entity并使用     @Inheritance(strategy = InheritanceType.JOINED)

1 个答案:

答案 0 :(得分:1)

  1. 首先使用基本字段

    创建基类
    @MappedSuperclass
    public class FooBase {
    
    @Id
    private Long id;
    private String name;
    
    }
    
  2. 在空类

    上将此课程映射到您的表格
    @Entity
    @Table(name = "foo")
    public class Foo  extends FooBase{
    
    }
    
  3. 将嵌套对象,集合,子资源映射到另一个类但是同一个表“foo”

    @Entity
    @Table(name = "foo")
    public class FooNested extends FooBase {
    
        @Fetch(FetchMode.SUBSELECT)
        @OneToMany(fetch = FetchType.EAGER)
        @JoinColumn(name = "foo_id", insertable = false, updatable = false)
        @ApiModelProperty(hidden = true)
        private List<Bar> barList;
        }
    
  4. 为每个实体创建一份报告

  5. 使用一个或另一个存储库来FETCh或不使用已实现的表/实体