【发布时间】:2020-10-20 01:23:13
【问题描述】:
我将SpringBoot 2.2.6 与JPA 和PostgreSQL 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 creation 与custom 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 设置为 true 和 nome 等于作为参数传递的字符串(我测试过但不起作用) .
也许在这种情况下我可以这样做:
public List<Nazione> findByActiveTrueAndNome(String nome);
但我要求这个更复杂的情况。我能以某种方式做到这一点吗??
谢谢大家
【问题讨论】:
标签: spring-boot hibernate jpa spring-data-jpa repository