Data Source integration with Quarkus

Dependencies

        <dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-orm-panache</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-postgresql</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-validator</artifactId>
</dependency>

Configuration

We need to provide the database name, driver name, username, and password. We can provide this information in the application.properties file.

#quarkus.datasource.url=jdbc:postgresql:employee_db
quarkus.datasource.driver=org.postgresql.Driver
quarkus.datasource.username=quarkus
quarkus.datasource.password=quarkus

Panache

Hibernate ORM is the de facto JPA implementation and offers you the full breadth of an Object Relational Mapper. It makes complex mappings possible, but it does not make simple and common mappings trivial. Hibernate ORM with Panache focuses on making your entities trivial and fun to write in Quarkus.

  • PanacheEntity
  • PanancheRepository

PanacheEntity

In this, we define all the database queries in the entity itself. We have to extend the PanacheEntity.

@Entity
public class Employee extends PanacheEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@NotBlank
private String firstName;
private String lastName;
private int age;
private int salary;
public Employee findById(int id) {
return findById(id);
}
public static void deleteById(int id) {
deleteById(id);
}
public static void deleteByFirstname(String name) {
delete("firstName", name);
}
public static void persist(Employee employee) {
persist(employee);
}
public static void update(Employee employee) {
update(employee);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}

list("age", 25); // JPQL
list("age = ?1 and firstName like ?2", 25, "%"+word+"%");
find("age = ?1 and firstName like ?2", 25, "%"+word+"%")
.page(pageIndex, 3).list();
@GET
public List<Employee> getAll() {
return Employee.listAll(Sort.by("firstName"));
}article
    @DELETE
@Path("/{id}")
@Produces(MediaType.TEXT_PLAIN)
@Transactional
public ResponseEntity delete(@PathParam("id")int id) {
Employee.deleteById(id);
return new ResponseEntity(HttpStatus.NO_CONTENT);
}

Panache Repository

The panache repository is almost the same as the Panache Entity. Here we are just segregating the database repository class from the entity. If we consider about Single responsibility principle then the Panache repository is a better way of executing Panache than the entity.

@ApplicationScoped
public class EmployeeRepository implements PanacheRepository<Employee> {
public Employee findById(int id) {
return findById(id);
}
public void deleteById(int id) {
deleteById(id);
}
public void deleteByFirstname(String name) {
delete("firstName", name);
}
public void persist(Employee employee) {
persist(employee);
}
public void update(Employee employee) {
update(employee);
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store