DirtyMind
DirtyMind

Reputation: 2581

JPA query on @MappedSuperClass. Fetch details of all the child class?

I am having 3 radio buttons 1.Car, 2.Bike, 3.Both. So if i select car it will fetch all the car details if i select 2 it will only fetch the car details till here i am able to achieve but how to fetch both the Car and bike details if I select 3rd radio button "both". In below example i want to do the same on selecting "both" it will fetch all the documents. What is the best solution for this?

    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);
    }

Or do I need to change the @MappedSuperClass to @Entity and use @Inheritance(strategy = InheritanceType.JOINED)

Upvotes: 0

Views: 1665

Answers (1)

Jose Boretto Blengino
Jose Boretto Blengino

Reputation: 886

  1. First Create a Base Class with the basic fields

    @MappedSuperclass
    public class FooBase {
    
    @Id
    private Long id;
    private String name;
    
    }
    
  2. Map this class to your table on empty class

    @Entity
    @Table(name = "foo")
    public class Foo  extends FooBase{
    
    }
    
  3. Map nested object, collections, subresources on another class but same table "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. Create One Reporitory for each Entity

  5. Use one or other repository to FETCh or Not the realted tables/entities

Upvotes: 1

Related Questions