73 lines
2.2 KiB
Java
Executable File
73 lines
2.2 KiB
Java
Executable File
package com.ankurm.hibernatedemo.namedquery;
|
|
|
|
import jakarta.persistence.Entity;
|
|
import jakarta.persistence.GeneratedValue;
|
|
import jakarta.persistence.GenerationType;
|
|
import jakarta.persistence.Id;
|
|
import jakarta.persistence.NamedQueries;
|
|
import jakarta.persistence.NamedQuery;
|
|
import jakarta.persistence.NamedNativeQueries;
|
|
import jakarta.persistence.NamedNativeQuery;
|
|
import jakarta.persistence.SqlResultSetMapping;
|
|
import jakarta.persistence.ConstructorResult;
|
|
import jakarta.persistence.ColumnResult;
|
|
import jakarta.persistence.Table;
|
|
|
|
/**
|
|
* Backs ankurm.com post 4877. Docs: 14-named-queries.md.
|
|
*
|
|
* <p>{@code Employee.findByName} is a valid JPA {@code @NamedQuery}, {@code Employee.byNativeDto}
|
|
* is a {@code @NamedNativeQuery} + {@code @SqlResultSetMapping} into {@link EmployeeDto} via
|
|
* {@code @ConstructorResult}.
|
|
*/
|
|
@Entity
|
|
@Table(name = "nq_employee")
|
|
@NamedQueries({
|
|
@NamedQuery(name = "Employee.findByName", query = "SELECT e FROM NqEmployee e WHERE e.firstName = :name"),
|
|
@NamedQuery(name = "Employee.findAllActive", query = "SELECT e FROM NqEmployee e WHERE e.status = 'ACTIVE' ORDER BY e.id DESC")
|
|
})
|
|
@NamedNativeQueries({
|
|
@NamedNativeQuery(
|
|
name = "Employee.byNativeDto",
|
|
query = "SELECT id, first_name AS firstName FROM nq_employee WHERE status = :status",
|
|
resultSetMapping = "EmployeeDtoMapping")
|
|
})
|
|
@SqlResultSetMapping(
|
|
name = "EmployeeDtoMapping",
|
|
classes = @ConstructorResult(
|
|
targetClass = EmployeeDto.class,
|
|
columns = {
|
|
@ColumnResult(name = "id", type = Long.class),
|
|
@ColumnResult(name = "firstName", type = String.class)
|
|
}))
|
|
public class NqEmployee {
|
|
|
|
@Id
|
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
|
private Long id;
|
|
|
|
private String firstName;
|
|
|
|
private String status;
|
|
|
|
protected NqEmployee() {
|
|
}
|
|
|
|
public NqEmployee(String firstName, String status) {
|
|
this.firstName = firstName;
|
|
this.status = status;
|
|
}
|
|
|
|
public Long getId() {
|
|
return id;
|
|
}
|
|
|
|
public String getFirstName() {
|
|
return firstName;
|
|
}
|
|
|
|
public String getStatus() {
|
|
return status;
|
|
}
|
|
}
|