我有一个类,它是一个实体和一个从它继承的子类,但它具有与父类名称相同的字段。
例如:
@Entity
class Parent {
String parentField;
}
@Entity
class Child extends Parent {
String parent;
}
现在,当我需要执行如下 JPA 查询时:
Root<Child> childClassRoot = criteriaBuilder.treat(parentRoot, Child.class);
criteriaQuery.select(parentRoot).where(
criteriaBuilder.like(childClassRoot.get("parent"), "%someText%")
)
对我不起作用,因为JPA认为parent不是child.parent,而是父类。当我将父级重命名为parentText 时,它工作正常。
因此,如果我修改子类并使用以下查询,那么它可以正常工作:
@Entity
class Child extends Parent {
String parentText;
}
和查询:
criteriaQuery.select(parentRoot).where(
criteriaBuilder.like(childClassRoot.get("parentText"), "%someText%")
)
有效,
但是有没有不需要重命名列名称的解决方案?我的意思是我可以重命名该列来解决这个问题,但是还有其他选择吗?
最佳答案
否
除了重命名没有其他解决办法。
关于java - JPA criteriaBuilder 中的列名称与父类名称冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54070078/