英文:
Inserting parameter inside HQL and fetch result using SpringBoot
问题
我有一个映射到Employee表的Employee类
public class Employee {
int id;
String name;
}
我已经编写了一个HQL查询,以获取各种参数的Employee,例如
Select x from Employee x where x.id = (:id) and x.name = (:name);
我已经创建了一个映射,其中键是id和name,值如下:
Map<String, Object> params = new HashMap<>();
params.put("id", 1);
params.put("name", "Testname");
如何在Spring Boot中发出请求,并使用Map params
中的值填充HQL查询,然后获取Employee对象的列表。
英文:
I have a Employee class mapped to Employee table
public Class Employee {
int id;
String name;
}
i have written a hql query to fetch Employee from various parameters..like
Select x from Employee x where x.id = (:id) and x.name = (:name);
I have create a map with key as id and name and values as below:
Map<String, Object> params = new HashMap<>();
params.put("id", 1);
params.put("name", "Testname");
How to make a request in Spring Boot and with a hql query populated with values in Map params
and fetch a list of Employee objects.
Edit: actually, I want to insert params Map with key and values in the HQL and get result.
答案1
得分: 1
如果你决定使用 HQL 查询,这个帖子应该能帮到你。
点击这里查看链接
还有一些更好地利用 Spring 和 JpaRepository 强大功能的方法。首先是在你的存储库中声明一个方法,包含你的变量名称,并在你的服务中使用该方法:
Repository
@Repository
Public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
Employee findByIdAndName(int id, String name);
}
Service
@Service
Public class EmployeeService {
@Autowired
private EmployeeRepository empRepo;
public Employee findByIdAndName(int id, String name) {
return empRepo.findByIdAndName(id, name);
}
}
如果你只有一个或两个条件,这样做很棒,但如果条件多于两个,方法名就会变得相当长。另一个建议是使用 Specifications。要使用这些,你还需要在你的存储库中扩展 JpaSpecificationExecutor。你可以创建一个单独的类来存放你的规格,但在这个例子中,我会把它放在服务中。
Repository
@Repository
Public interface EmployeeRepository extends JpaRepository<Employee, Integer>, JpaSpecificationExecutor<Employee> {}
Service
@Service
Public class EmployeeService {
@Autowired
private EmployeeRepository empRepo;
public Employee findByIdAndName(int id, String name) {
return empRepo.find(idEquals(id).and(nameEquals(name)));
}
private Specification<Employee> idEquals(int id) {
return (root, query, cb) -> cb.equal(root.get(Employee_.id), id);
}
private Specification<Employee> nameEquals(String name) {
return (root, query, cb) -> cb.equal(root.get(Employee_.name), name);
}
}
请注意,要使用静态元模型 Employee_,你将需要额外的依赖,具体来说是 org.hibernate.jpamodelgen 如果我没记错的话。
英文:
If you’re set on using HQL queries, this thread should be able to help you.
enter link description here
There are also a couple ways to do this that better utilize the power of Spring and JpaRepository. The first is to declare a method in your repository that includes your variable names and use that method in your service:
Repository
@Repository
Public interface EmployeeRepository extends JpaRepository<Employee, Integer> {
Employee findByIdAndName(int id, String name);
}
Service
@Service
Public class EmployeeService {
@Autowired
private EmployeeRepository empRepo;
public Employee findByIdAndName(int id, String name) {
return empRepo.findByIdAndName(id, name);
}
}
This is great if you only have one or two criteria, but more than that and the method name starts to get rather long. The other suggestion is to use Specifications. To use these, you need to also extend JpaSpecificationExecutor in your repo. You can create a separate class to house your specifications, but for this example I’m going to put it in the service.
Repository
@Repository
Public interface EmployeeRepository extends JpaRepository<Employee, Integer>, JpaSpecificationExecutor<Employee> {}
Service
@Service
Public class EmployeeService {
@Autowired
private EmployeeRepository empRepo;
public Employee findByIdAndName(int id, String name) {
return empRepo.find(idEquals(id).and(nameEquals(name)));
}
private Specification<Employee> idEquals(int id) {
return (root, query, cb) -> cb.equal(root.get(Employee_.id), id);
}
private Specification<Employee> nameEquals(String name) {
return (root, query, cb) -> cb.equal(root.get(Employee_.name), name);
}
}
Note that to use the static metamodel Employee_ you will need extra dependencies, specifically org.hibernate.jpamodelgen if memory serves.
答案2
得分: 0
是不是可以使用Spring JPA
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
@Query("SELECT e FROM Employee e WHERE e.id = :id AND e.name = :name")
List<Employee> findEmployeesByIdAndName(@Param("id") int id, @Param("name") String name);
}
英文:
Is it possible to use Spring JPA
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
@Query("SELECT e FROM Employee e WHERE e.id = :id AND e.name = :name")
List<Employee> findEmployeesByIdAndName(@Param("id") int id, @Param("name") String name);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论