adam bien's blog

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.