Java Hibernate criteriaQuery,仅筛选子类权限
如果提供,则存在下一个层次结构:Java Hibernate criteriaQuery,仅筛选子类权限,java,hibernate,spring-data,Java,Hibernate,Spring Data,如果提供,则存在下一个层次结构: @Inheritance(strategy = InheritanceType.JOINED) Class A{ String name; } Class B extends A{ boolean isActive; } 然后,我需要根据几个标准进行筛选,包括isActive 我决定处理规范。 但面临一个问题。当我通过isActive=false进行过滤时,我将获得所有具有isActive=false的子类实体。完全忽略父类实体。我的预期结果是从父类
@Inheritance(strategy = InheritanceType.JOINED)
Class A{
String name;
}
Class B extends A{
boolean isActive;
}
然后,我需要根据几个标准进行筛选,包括isActive
我决定处理规范
。
但面临一个问题。当我通过
isActive=false
进行过滤时,我将获得所有具有isActive=false
的子类实体。完全忽略父类实体。我的预期结果是从父类中获取所有实体,并从suClass中过滤实体。有人知道怎么处理吗 我认为您的案例不需要说明。我猜你想通过孩子的财产得到父母。我建议您对布尔属性使用Boolean
类,因为它将区分null
和false
值。要按类选择子级,可以使用JPQLTYPE
运算符。您可以在这里找到一个关于如何操作的示例:
父类:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Getter
@Setter
public class Foo {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
}
儿童1班:
@Entity
@Getter
@Setter
public class Bar extends Foo {
private Boolean active;
}
儿童2班:
@Entity
@Getter
@Setter
public class Qux extends Foo {
private Boolean done;
}
存储库:
public interface FooRepository extends JpaRepository<Foo, Long> {
@Query("select f from Foo f where TYPE(f) = Bar and f.active = false")
List<Foo> findByActive();
}
希望它能帮助你
@RunWith(SpringRunner.class)
@SpringBootTest
public class FooTests {
@Autowired
private FooRepository fooRepository;
@Before
public void setup() {
Bar bar1 = new Bar();
bar1.setActive(false);
Foo foo1 = bar1;
foo1.setName("foo1");
fooRepository.save(foo1);
Bar bar2 = new Bar();
Foo foo2 = bar2;
foo2.setName("foo2");
fooRepository.save(foo2);
Foo foo3 = new Qux();
foo3.setName("foo3");
fooRepository.save(foo3);
}
@Test
public void test() {
System.out.println(fooRepository.findByActive());
}
}