Quarkus - Using Blaze-Persistence

Blaze-Persistence offers a fluent query builder API on top of JPA with a deep Hibernate ORM integration that enables the use of advanced SQL features like Common Table Expressions while staying in the realm of the JPA model.

On top of that, the Blaze-Persistence Entity-View module allows for DTO definitions that can be applied to business logic queries which are then transformed to optimized queries that only fetch the data that is needed to construct the DTO instances. The same DTO definitions can further be used for applying database updates, leading to a great reduction in boilerplate code and removing the need for object mapping tools.

This extension is developed by a third party and is part of the Quarkus Platform.

Setting up and configuring Blaze-Persistence

The extension comes with default producers for CriteriaBuilderFactory and EntityViewManager that work out of the box given a working Hibernate ORM configuration. For customization, overriding of the default producers is possible via the standard mechanism as documented in the Quarkus CDI reference. This is needed if you need to set custom Blaze-Persistence properties.

In Quarkus, you just need to:

  • @Inject CriteriaBuilderFactory or EntityViewManager and use it

  • annotate your entity views with @EntityView and any other mapping annotation as usual

Add the following dependencies to your project:

  • the Blaze-Persistence extension: com.blazebit:blaze-persistence-integration-quarkus

  • further Blaze-Persistence integrations as needed:

    • blaze-persistence-integration-jackson for Jackson

    • blaze-persistence-integration-jaxrs for JAX-RS

Example dependencies using Maven

  1. <dependencies>
  2. <!-- Blaze-Persistence specific dependencies -->
  3. <dependency>
  4. <groupId>com.blazebit</groupId>
  5. <artifactId>blaze-persistence-integration-quarkus</artifactId>
  6. </dependency>
  7. <dependency>
  8. <groupId>com.blazebit</groupId>
  9. <artifactId>blaze-persistence-integration-hibernate-5.4</artifactId>
  10. <scope>runtime</scope>
  11. </dependency>
  12. </dependencies>

The use in native images requires a dependency on the entity view annotation processor that may be extracted into a separate native profile:

  1. <profiles>
  2. <profile>
  3. <id>native</id>
  4. <dependencies>
  5. <dependency>
  6. <groupId>com.blazebit</groupId>
  7. <artifactId>blaze-persistence-entity-view-processor</artifactId>
  8. <scope>provided</scope>
  9. </dependency>
  10. </dependencies>
  11. </profile>
  12. </profiles>

A CriteriaBuilderFactory and an EntityViewManager will be created based on the configured EntityManagerFactory as provided by the Hibernate-ORM extension.

You can then access these beans via injection:

Example application bean using Hibernate

  1. @ApplicationScoped
  2. public class SantaClausService {
  3. @Inject
  4. EntityManager em; (1)
  5. @Inject
  6. CriteriaBuilderFactory cbf; (2)
  7. @Inject
  8. EntityViewManager evm; (3)
  9. @Transactional (4)
  10. public List<GiftView> findAllGifts() {
  11. CriteriaBuilder<Gift> cb = cbf.create(em, Gift.class);
  12. return evm.applySetting(EntityViewSetting.create(GiftView.class), cb).getResultList();
  13. }
  14. }
1Inject the EntityManager
2Inject the CriteriaBuilderFactory
3Inject the EntityViewManager
4Mark your CDI bean method as @Transactional so that a transaction is started or joined.

Example Entity

  1. @Entity
  2. public class Gift {
  3. private Long id;
  4. private String name;
  5. private String description;
  6. @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="giftSeq")
  7. public Long getId() {
  8. return id;
  9. }
  10. public void setId(Long id) {
  11. this.id = id;
  12. }
  13. public String getName() {
  14. return name;
  15. }
  16. public void setName(String name) {
  17. this.name = name;
  18. }
  19. public String getDescription() {
  20. return description;
  21. }
  22. public void setDescription(String description) {
  23. this.description = description;
  24. }
  25. }

Example Entity-View

  1. @EntityView(Gift.class)
  2. public interface GiftView {
  3. @IdMapping
  4. Long getId();
  5. String getName();
  6. }

Example updatable Entity-View

  1. @UpdatableEntityView
  2. @CreatableEntityView
  3. @EntityView(Gift.class)
  4. public interface GiftUpdateView extends GiftView {
  5. void setName(String name);
  6. }

Example JAX-RS Resource

  1. @Path("/gifts")
  2. public class GiftResource {
  3. @Inject
  4. EntityManager entityManager;
  5. @Inject
  6. EntityViewManager entityViewManager;
  7. @Inject
  8. SantaClausService santaClausService;
  9. @POST
  10. @Consumes(MediaType.APPLICATION_JSON)
  11. @Produces(MediaType.APPLICATION_JSON)
  12. @Transactional
  13. public Response createGift(GiftUpdateView view) {
  14. entityViewManager.save(entityManager, view);
  15. return Response.created(URI.create("/gifts/" + view.getId())).build();
  16. }
  17. @GET
  18. @Produces(MediaType.APPLICATION_JSON)
  19. public List<GiftView> getGifts() {
  20. return santaClausService.findAllGifts();
  21. }
  22. @PUT
  23. @Path("{id}")
  24. @Consumes(MediaType.APPLICATION_JSON)
  25. @Produces(MediaType.APPLICATION_JSON)
  26. @Transactional
  27. public GiftView updateGift(@EntityViewId("id") GiftUpdateView view) {
  28. evm.save(em, view);
  29. return evm.find(em, GiftView.class, view.getId());
  30. }
  31. @Path("{id"})
  32. @GET
  33. @Produces(MediaType.APPLICATION_JSON)
  34. public GiftView getGift(@PathParam("id") Long id) {
  35. return return entityViewManager.find(entityManager, GiftView.class, view.getId());
  36. }
  37. }

Blaze-Persistence configuration properties

There are various optional properties useful to refine your EntityViewManager and CriteriaBuilderFactory or guide guesses of Quarkus.

There are no required properties, as long as the Hibernate ORM extension is configured properly.

When no property is set, the Blaze-Persistence defaults apply.

The configuration properties listed here allow you to override such defaults, and customize and tune various aspects.

Limitations

Apache Derby

Blaze-Persistence currently does not come with support for Apache Derby. This limitation could be lifted in the future, if there’s a compelling need for it and if someone contributes it.