Data Source integration with Quarkus

Understanding the power of 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

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

Panache

@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);
}
@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