Code Shrinking with Quarkus and Panache ORM

With Panache ORM and Quarkus a JSON-B / JPA entity:


package workshops;

import java.time.LocalDate;
import javax.persistence.Entity;
import io.quarkus.hibernate.orm.panache.PanacheEntity;

@Entity
public class Workshop extends PanacheEntity{

    public String name;
    public LocalDate date;
}    
can be directly exposed via a JAX-RS resource:

package workshops;

import java.util.List;

import javax.transaction.Transactional;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Produces(MediaType.APPLICATION_JSON)
@Path("/workshops")
public class WorkshopsResource {

    @GET
    public List<Workshop> workshops() {
        return Workshop.listAll();
    }

    @POST
    @Transactional
    @Consumes(MediaType.APPLICATION_JSON)
    public void save(Workshop workshop) {
        workshop.persist();
    }
}    

without any direct reference to an EntityManager or declaration of persistence.xml.

The data source is configured via MicroProfile configuration:


quarkus.datasource.url = jdbc:postgresql://localhost:5432/postgres
quarkus.datasource.driver = org.postgresql.Driver
quarkus.datasource.username = airhacks
quarkus.datasource.password = airhacks
quarkus.hibernate-orm.database.generation = drop-and-create
See it in action (and from scratch):

See you at Web, MicroProfile and Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.

Comments:

Panache: Flair, Style :-)

https://www.dictionary.com/browse/panache?s=t

Posted by John Clingan on October 22, 2019 at 01:58 PM CEST #

Knowing some quarkus developers :-), I thought Panache was meant as defined here:
https://en.wikipedia.org/wiki/Shandy#Panaché

Posted by Adam Bien on October 22, 2019 at 05:10 PM CEST #

It looks line Panache Is Hibernate combined with the Active Record pattern.

https://www.martinfowler.com/eaaCatalog/activeRecord.html

Posted by Danilo Piazzalunga on November 05, 2019 at 09:00 AM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments
License