Search code examples
quarkusquarkus-panache

Create SQL view with panache


I'm using

<dependency>
     <groupId>io.quarkus</groupId>
     <artifactId>quarkus-hibernate-reactive-panache</artifactId>
</dependency>

and I would like to generate an SQL view in the same way we generate tables, specifically as follows:

@Entity
public class Invoice extends PanacheEntityBase {
...
}

I've tried that way but with no really success, I can't found anything on this in quarkus' documentation:

@Entity
@Subselect("SELECT i.id as invoiceId, i.appointmentIds FROM invoice i")
public class BilledAppointments extends PanacheEntityBase {
    @Column
    public Long invoiceId;

    @Column
    public String appointmentsIds;
}

Solution

  • I figured out that views are not managed by frameworks but only by databases. So it is not really possible to map a view to a "classic" entity, since Panache will interprate this as a table and create one. Moreover, if you create a view manually in your database that references one of your entities, it may be deleted if you adopt the drop-and-create strategy.

    A solution is:

    @Entity
    public class MyTable extends PanacheEntityBase {}
    
    @Entity
    @Subselect("SELECT * FROM MyTable")
    @Synchronize("MyTable")
    public class MyView extends PanacheEntityBase {}