您好,我是 JPA 和 Criteria API 的新手。 我正在尝试获取单个列的不同值(尝试仅获取 TestId 的不同值)。我的数据库中有下表。
__________________________________
|TestId(Pk) | TestCol(PK) | TestEx|
__________________________________
我有以下类(class) 模型类
@Data
@Entity
@Table(schema = "TEST", name = "TEST_TYPE")
public class Test {
@EmbeddedId
private TestPK id;
@Column(nmae = "TestEX")
private double testEx
}
@Data
@Embeddable
public class TestPK {
@Column(name = "TestId")
private String testId;
@Column(name="TestCol")
private String testcol
}
存储库类
public class TestRepoImpl {
@PersistenceContext
EntityManager em;
@Override
public List<Test> getData() {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Test> cq = cb.createQuery(Test.class);
Root<Test> root = cq.from(Test.class);
// cq.get(Test_.id).get(TestPK_.testId);
// cq.select(root);
cq.multiselect(root.get(Test_.id).get(TestPK_.testId));
cq.distinct(true);
// List<Tuple> ts = em.createQuery(cq).getResultList();
List<Test> data = em.createQuery(cq).getResultList();
return data;
}
}
我遇到以下错误。
部分对象查询不允许维护缓存或编辑。 您必须使用 dontMaintainCache()。
最佳答案
请尝试 here 中提到的此选项((org.eclipse.persistence.jpa.JpaQuery)query).getDatabaseQuery().dontMaintainCache();
关于java - 如何在 Criteria API(JPA) 中获取单列的不同值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46620620/