Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 使用鉴别器值查询数据库_Hibernate_Spring Boot_Inheritance_Spring Data Jpa_Single Table Inheritance - Fatal编程技术网

Hibernate 使用鉴别器值查询数据库

Hibernate 使用鉴别器值查询数据库,hibernate,spring-boot,inheritance,spring-data-jpa,single-table-inheritance,Hibernate,Spring Boot,Inheritance,Spring Data Jpa,Single Table Inheritance,我使用的是单表继承策略。我想通过使用鉴别器类型对数据库进行过滤来执行搜索。如何在JPA中编写函数来执行此操作 使用findBy…方法定义方法的常规方法不会产生结果 这是我的家长班 @Entity @Inheritance(strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn( name="Leave_Type", discriminatorType=DiscriminatorType.STRING

我使用的是单表继承策略。我想通过使用鉴别器类型对数据库进行过滤来执行搜索。如何在JPA中编写函数来执行此操作

使用
findBy…
方法定义方法的常规方法不会产生结果

这是我的家长班

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
        name="Leave_Type",
        discriminatorType=DiscriminatorType.STRING
        )
public class LeaveQuota {
// the fields and the required methods
}
下面是两个子实体

@Entity
@DiscriminatorValue("Annual")
public class AnnualLeave extends LeaveQuota {
// the fields and the required methods
}

@Entity
@DiscriminatorValue("Casual")
public class CasualLeave extends LeaveQuota {
// the fields and the required methods
}
我想通过分别过滤年假和临时假来查询数据库。这意味着当我搜索年假时,应该检索鉴别器列中所有值为“annual”的记录。我如何实现这一点。
提前谢谢

为AnnualLeave.java创建一个存储库,即AnnualLeaveRepo.java和CausualLeave.java,如下所示:

AnnualLeaveRepo.java

@Repository
public interface AnnualLeaveRepo extends JpaRepository<AnnualLeave,Integer>   {

    @Query("from AnnualLeave")
    public List<AnnualLeave> getLeaves();

    //consider noOfLeave is member of AnnualLeave.java class
    public List<AnnualLeave> findByNoOfLeave(int noOfLeave); 

}
@存储库
公共接口AnnualAverePO扩展了JpaRepository{
@查询(“来自AnnualLeave”)
公共列表getLeaves();
//假设nooplifeve是AnnualLeave.java类的成员
公共列表findbynoofleve(int nooplifeve);
}
CausalLeaveRepo.java

@Repository
public interface CausalLeaveRepo extends JpaRepository<CausalLeave,Integer>   {

    @Query("from CausalLeave")
    public List<CausalLeave> getLeaves();
}
@存储库
公共接口CausalLeaveRepo扩展了JpaRepository{
@查询(“来自CausalLeave”)
公共列表getLeaves();
}
现在,当您使用findAll()getLeaves()findbynoofliveve(int)方法或AnnualLeaveRepo类的任何其他自定义抽象方法时,它将自动使用Leave\u Type=“Annual”过滤结果

类似地,当您使用CausalLeaveRepo类的findAll()getLeaves()方法或任何其他自定义抽象方法时,它将使用Leave_Type=“Causary”自动过滤结果

您不必显式地过滤掉

注意
如果类中有任何属性与LeaveQuota实体或其继承的实体具有关系(@OneToMany等…),那么不要忘记对这些属性使用@JsonIgnore注释。否则会出现stackoverflow错误

请在执行查询和sql输出的地方共享代码片段,确定“Leve_Type”列的第一个字母是大写的吗?我还没有编写代码。这就是我所要求的关于如何实施它的问题。“leve_Type”在数据库中使用小写字母保存。这里使用的策略是单_表。那么,我是否能够创建一个数据库中没有单独表的年假存储库?是的,您可以创建两个存储库,即年假和事假,同时使用单表策略。我尝试了上述方法。但收到的结果会无限重复。我是说我有一个好的。我已经上传了GitHub上的示例。请参考它,您可以在本地计算机上运行它。对application.properties进行必要的更改。