diff --git a/persistence-modules/spring-data-jpa-annotations-2/src/main/java/com/baeldung/queryhint/Employee.java b/persistence-modules/spring-data-jpa-annotations-2/src/main/java/com/baeldung/queryhint/Employee.java new file mode 100644 index 0000000000..ba183675a5 --- /dev/null +++ b/persistence-modules/spring-data-jpa-annotations-2/src/main/java/com/baeldung/queryhint/Employee.java @@ -0,0 +1,86 @@ +package com.baeldung.queryhint; + +import java.sql.Date; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; +import javax.persistence.QueryHint; +import javax.persistence.Table; + +import org.springframework.data.jpa.repository.QueryHints; + +@Entity +@Table(name = "app_user") +@NamedQueries({ @NamedQuery(name = "selectEmployee", query = "SELECT e FROM Employee e", hints = @QueryHint(name = "org.hibernate.fetchSize", value = "50")) }) +public class Employee { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + private String username; + private String password; + private String gender; + private String name; + private Date joinDate; + private int age; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getGender() { + return gender; + } + + public void setGender(String gender) { + this.gender = gender; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Date getJoinDate() { + return joinDate; + } + + public void setJoinDate(Date joinDate) { + this.joinDate = joinDate; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } +} \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-annotations-2/src/main/java/com/baeldung/queryhint/EmployeeApplication.java b/persistence-modules/spring-data-jpa-annotations-2/src/main/java/com/baeldung/queryhint/EmployeeApplication.java new file mode 100644 index 0000000000..df3ca42e4c --- /dev/null +++ b/persistence-modules/spring-data-jpa-annotations-2/src/main/java/com/baeldung/queryhint/EmployeeApplication.java @@ -0,0 +1,12 @@ +package com.baeldung.queryhint; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class EmployeeApplication { + + public static void main(String[] args) { + SpringApplication.run(EmployeeApplication.class); + } +} diff --git a/persistence-modules/spring-data-jpa-annotations-2/src/main/java/com/baeldung/queryhint/EmployeeRepository.java b/persistence-modules/spring-data-jpa-annotations-2/src/main/java/com/baeldung/queryhint/EmployeeRepository.java new file mode 100644 index 0000000000..9c0b79cfd4 --- /dev/null +++ b/persistence-modules/spring-data-jpa-annotations-2/src/main/java/com/baeldung/queryhint/EmployeeRepository.java @@ -0,0 +1,29 @@ +package com.baeldung.queryhint; + +import java.util.List; + +import javax.persistence.QueryHint; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.QueryHints; +import org.springframework.stereotype.Repository; + +@Repository +public interface EmployeeRepository extends JpaRepository { + + @QueryHints(value = { @QueryHint(name = "org.hibernate.fetchSize", value = "50") }) + List findByGender(String gender); + + @QueryHints(value = { @QueryHint(name = "javax.persistence.query.timeout", value = "5000") }) + List findActiveEmployees(long inactiveDaysThreshold); + + @QueryHints(value = { @QueryHint(name = "jakarta.persistence.cache.retrieveMode", value = "USE"), + @QueryHint(name = "jakarta.persistence.cache.storeMode", value = "USE") }) + List findEmployeesByName(String name); + + @QueryHints(@QueryHint(name = "org.hibernate.readOnly", value = "true")) + Employee findByUsername(String username); + + @QueryHints(value = { @QueryHint(name = "org.hibernate.comment", value = "Retrieve employee older than specified age\"") }) + List findByAgeGreaterThan(int age); +}