具有单表继承的子类的 JPA 查询
JPA Query on sublclass with SingleTable inheritance
假设我有以下实体:
@Entity
public class Container
{
@OneToMany
Set<AbstractElement> elements;
//getter setter and other attributes...
}
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "CLASS_CODE", discriminatorType = DiscriminatorType.STRING, length = 10)
public abstract class AbstractElement<T> extends AbstractEntity
{
@Transient
T value;
//getter setter and other attributes...
}
@DiscriminatorValue(value = "BOOL")
@Entity
public class BooleanElement extends AbstractElement<Boolean>
{
/**
* {@inheritDoc}
*/
@Column(name = "VALUE_BOOL")
@Override
public Boolean getValue()
{
return super.getValue();
}
}
问题是这样的:
如何从 class CONTAINER 开始对 BooleanElement 的值执行 jpa 条件查询?
到目前为止我实际拥有的是:
CriteriaQuery<Container> criteriaQuery = criteriaBuilder.createQuery(Container.class);
Root<Container> from = criteriaQuery.from(Container.class);
criteriaQuery.select(from);
from = from.join("elements");
Predicate pred = criteriaBuilder.equal(criteriaBuilder.treat(from ,BooleanElement.class).get("value"), FOO);
//etc......
此时的例外是"there is not VALUE attributes on the AbstractEntity"。
提前致谢。
在此特定情况下,Treat 运算符不起作用。
连接上的 as 运算符它仍然不太清楚如何实现:我在第 3 行执行此操作时得到 class 强制转换异常:
CriteriaQuery<AbstractElement> criteriaQuery = criteriaBuilder.createQuery(AbstractElement.class);
Root<AbstractElement> rootAbstract = criteriaQuery.from(AbstractElement.class);
Path predPath = (Path)rootAbstract.as(BooleanElement.class);
predPath.get("value");
执行此类查询的唯一方法是执行子查询
或创建一个额外的 "from" 子句:
Root<BooleanElement> from2 = criteriaQuery.from(BooleanElement.class);
Predicate joinPredicate = criteriaBuilder.equal(from, from2);
....
假设我有以下实体:
@Entity
public class Container
{
@OneToMany
Set<AbstractElement> elements;
//getter setter and other attributes...
}
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "CLASS_CODE", discriminatorType = DiscriminatorType.STRING, length = 10)
public abstract class AbstractElement<T> extends AbstractEntity
{
@Transient
T value;
//getter setter and other attributes...
}
@DiscriminatorValue(value = "BOOL")
@Entity
public class BooleanElement extends AbstractElement<Boolean>
{
/**
* {@inheritDoc}
*/
@Column(name = "VALUE_BOOL")
@Override
public Boolean getValue()
{
return super.getValue();
}
}
问题是这样的:
如何从 class CONTAINER 开始对 BooleanElement 的值执行 jpa 条件查询?
到目前为止我实际拥有的是:
CriteriaQuery<Container> criteriaQuery = criteriaBuilder.createQuery(Container.class);
Root<Container> from = criteriaQuery.from(Container.class);
criteriaQuery.select(from);
from = from.join("elements");
Predicate pred = criteriaBuilder.equal(criteriaBuilder.treat(from ,BooleanElement.class).get("value"), FOO);
//etc......
此时的例外是"there is not VALUE attributes on the AbstractEntity"。
提前致谢。
在此特定情况下,Treat 运算符不起作用。 连接上的 as 运算符它仍然不太清楚如何实现:我在第 3 行执行此操作时得到 class 强制转换异常:
CriteriaQuery<AbstractElement> criteriaQuery = criteriaBuilder.createQuery(AbstractElement.class);
Root<AbstractElement> rootAbstract = criteriaQuery.from(AbstractElement.class);
Path predPath = (Path)rootAbstract.as(BooleanElement.class);
predPath.get("value");
执行此类查询的唯一方法是执行子查询 或创建一个额外的 "from" 子句:
Root<BooleanElement> from2 = criteriaQuery.from(BooleanElement.class);
Predicate joinPredicate = criteriaBuilder.equal(from, from2);
....