【问题标题】:JPA findBy Query creation plus custom parameterJPA findBy 查询创建加自定义参数
【发布时间】:2020-10-20 01:23:13
【问题描述】:

我将SpringBoot 2.2.6JPAPostgreSQL DBMS 一起使用。

假设我有entity 国家如下:

@Entity
@Data
@Table(name = "nation")
public class Nazione {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Integer id;

  private String nome;
  private Boolean active;
}

比我有Repository:

@Repository
public interface NazioneRepository extends JpaRepository<Nazione, Integer> {
  public List<Nazione> findByActiveTrue();
}

使用JPA Query Creation 方法findByActiveTrue() 检索所有国家 并将active 设置为true

我想知道是否可以将JPA Query creationcustom query 混合使用,例如:

@Repository
public interface NazioneRepository extends JpaRepository<Nazione, Integer> {

  @Query("select n from Nazione n where n.nome = ?1")
  public List<Nazione> findByActiveTrue(String nome);
}

有了这个,我想检索所有 Nation 并将 active 设置为 truenome 等于作为参数传递的字符串(我测试过但不起作用) .

也许在这种情况下我可以这样做:

public List<Nazione> findByActiveTrueAndNome(String nome);

但我要求这个更复杂的情况。我能以某种方式做到这一点吗??

谢谢大家

【问题讨论】:

    标签: spring-boot hibernate jpa spring-data-jpa repository


    【解决方案1】:

    我不知道确切的要求,但我认为你可以做一件事。您可以获取所有活跃的国家,然后使用这样的原生查询按nome 对其进行排序:

    • 将 EntityManager 传递给方法并在第一次执行后使用它。
    • 下面的代码是 scala 的,尝试用 java 做。
    import javax.persistence.EntityManager
    
    def findByActiveTrue(em: EntityManager, nome: String) = {
        val sql = s"select * from Nazione n where n.nome = $nome"
        val value = em.createNativeQuery(sql)
        val result = nativeQuery.getResultList
        result
      }
    

    现在您可以对活跃的国家数据进行任何操作。检查here中EntityManager的方法

    【讨论】:

      【解决方案2】:

      试试

      public List<Nazione> findByActiveTrueAndNomeEquals(String nome);
      
      

      或者如果您想要更多“本机查询”:

      @Query("select n from Nazione n where n.nome = ?1 and active=true")
        public List<Nazione> findByActiveTrue(String nome);
      

      您不能将 @Query 与 auto 混合使用。

      有时我使用 jpa 规范(用于具有许多过滤器的端点。示例:

      public class NazioneSpec {
      
          private String nome;
      
          public Specification<SalesInvoice> getSpecification() {
              return (root, query, cb) -> {
                  List<Predicate> predicates = new ArrayList<>();
                  query.distinct(true);
      
                  if (nome != null) {
                      predicates.add(cb.equal(root.get(Nazione_.nome), nome));
                  }
                  predicates.add(cb.equal(root.get(Nazione_.active), true));
      
                  return cb.and(predicates.toArray(new Predicate[0]));
              };
          }
      }
      

      然后,你可以使用:

          @GetMapping(value = "/nazioneList")
          public List<Nazione> getList(NazioneSpec spec) {
             return repository.findAll(spec.getSpecification());
          }
      

      【讨论】:

      • 谢谢你!我想@Query 会覆盖 JPA 创建的那个...
      猜你喜欢
      • 2012-03-09
      • 1970-01-01
      • 2020-02-21
      • 2017-08-15
      • 2020-08-12
      • 2019-09-25
      • 1970-01-01
      • 2018-04-27
      • 2020-02-11
      相关资源
      最近更新 更多