2

I have to extract data for statistic purpose. I've created a native query and used @SqlResultSetMapping to map the resultset to an object. Hibernate needs to declare this class (Elaboration) as @Entity BUT IS NOT A TABLE, and I don't want a table because I have only to extract data on the fly when needed.

The code works fine but the gitlab pipeline fails during validation with

schemaManagementException: Schema-validation: missing table [elaboration].

Here my code so far:

    SqlResultSetMapping(name="ValueMapping",
                     classes={
                       @ConstructorResult(
                         targetClass=Elaboration.class,
                         columns={
                           @ColumnResult(name="areadesc", type=String.class),
                           @ColumnResult(name="subsectordesc", type=String.class),
                           @ColumnResult(name="eurovalue", type=BigDecimal.class),
                           @ColumnResult(name="eurotch", type=BigDecimal.class),
                         }
                       )
                     })
    @Entity 
    public class Elaboration{
      @Id
      private Long id;
      private String areadesc;
      private String subsectordesc;
      private Integer dossiercount;
      private BigDecimal eurovalue;
      private BigDecimal eurotch; 

    ....

and the custom query:

        String statisticValueQuery = "select  a.mdescr as areadesc, s.mdescr as subsectordesc, sum(euro_value) as eurovalue, 
    sum(euro_value_tch) as eurotch " +
                                   "from dossier d " +
                                   "join dossier_document dd on d.id = dd.dossier_id " +
                                   "join dossier_country dc on d.id = dc.dossier_id " +
                                   "join country c on dc.country_id = c.id " +
                                   "join area a on c.area_id = a.id " +
                                   "join dossier_subsector ds on d.id = ds.dossier_id " +
                                   "join subsector s on ds.subsector_id = s.id " +
                                   "where dd.document_id = :document  " +
                                   "and d.submission_date >= :startdate  and d.submission_date <= :enddate " +
                                   "group by s.id, a.id;";

      public List<Elaboration> getValueElaboration(ElaborationRequestDTO elaborationRequestDTO){

        Query resultMapping = em.createNativeQuery(statisticValueQuery, "ValueMapping");
        resultMapping.setParameter("startdate", elaborationRequestDTO.getElaborateFromEquals());
        resultMapping.setParameter("enddate", elaborationRequestDTO.getElaborateToEquals());
        resultMapping.setParameter("document", elaborationRequestDTO.getDocumentIdEquals());
        return resultMapping.getResultList();

Is there a way to pass the validation test? Thanks

SternK
  • 11,649
  • 22
  • 32
  • 46

1 Answers1

2

This is wrong statement.

Hibernate needs to declare this class (Elaboration) as @Entity

You should just put your @SqlResultSetMapping declaration above some @Entity but it can be some other entity not related to the Elaboration.

@SqlResultSetMapping(name="ValueMapping",
                 classes={
                   @ConstructorResult(
                     targetClass=Elaboration.class,
                     columns={
                       @ColumnResult(name="areadesc", type=String.class),
                       @ColumnResult(name="subsectordesc", type=String.class),
                       @ColumnResult(name="eurovalue", type=BigDecimal.class),
                       @ColumnResult(name="eurotch", type=BigDecimal.class),
                     }
                   )
                 })
@Entity 
public class SomeEntity {

}

And if Elaboration is not an entity you should not annotate it as such.

SternK
  • 11,649
  • 22
  • 32
  • 46