[英]Spring data JPA with dynamic where clause
I have a base Repository, say IBaseRepository
which is 我有一个基本的存储库,例如
IBaseRepository
public interface IBaseRepository<T extends BaseEntity<PK>, PK extends Serializable>
extends JpaRepository<T, PK>, JpaSpecificationExecutor<T> {
}
now every repository class, for example UserRepository
extends from this base repository. 现在,每个存储库类(例如
UserRepository
从该基本存储库扩展。 How can I add a general method like 我如何添加像
T findOne(String filter, Map<String, Object> params);
for all inherited classes so that calling 对于所有继承的类,以便调用
Map<String,Object> params = new HashMap<String,Object>();
params.put("username","Lord");
params.put("locked",Status.LOCKED);
userRepo.findeOne("username = :username AND status = :locked",params);
return me a single record with dynamic where clause. 返回我一条带有动态where子句的记录。
You can do the Following 您可以执行以下操作
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.NoRepositoryBean;
import java.io.Serializable;
import java.util.Map;
/**
* Created by shazi on 1/11/2017.
*/
@NoRepositoryBean
public interface IBaseRepository<T, ID extends Serializable> extends JpaRepository<T, ID>, JpaSpecificationExecutor<T> {
T findOne(String filter, Map<String, Object> params);
}
And Implement it as follows. 并实现如下。
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.io.Serializable;
import java.util.Map;
/**
* Created by shazi on 1/11/2017.
*/
public class BaseRepositoryImpl<T, ID extends Serializable>
extends SimpleJpaRepository<T, ID> implements IBaseRepository<T, ID> {
private final EntityManager entityManager;
private final JpaEntityInformation entityInformation;
public BaseRepositoryImpl(JpaEntityInformation entityInformation,
EntityManager entityManager) {
super(entityInformation, entityManager);
// Keep the EntityManager around to used from the newly introduced methods.
this.entityManager = entityManager;
this.entityInformation = entityInformation;
}
@Override
public T findOne(String filter, Map<String, Object> params) {
final String jpql = "FROM " + entityInformation.getEntityName() + " WHERE " + filter;
Query query = entityManager.createQuery(jpql);
for (Map.Entry<String, Object> value:params.entrySet()) {
query.setParameter(value.getKey(), value.getValue());
}
return (T) query.getSingleResult();
}
}
And configure it as follows 并如下配置
@Configuration
@EnableJpaRepositories(repositoryBaseClass = BaseRepositoryImpl.class)
@EnableTransactionManagement
public class RepoConfig {
or in XML 或XML
<repositories base-class="….BaseRepositoryImpl" />
Finally you can use it as follows; 最后,您可以按以下方式使用它;
User found = userRepository.findOne("name = :name", Collections.singletonMap("name", "name"));
But you have to make sure that your query WHERE is such that the Query will always return 1 result only. 但是,您必须确保查询WHERE是这样,查询将始终仅返回1个结果。 See this post
看到这个帖子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.