问题描述
我有两个没有建模关系的表:
I have two tables with no modeled relation:
具有列的表comm
:
name
date
code
具有列的表persondesc
:
code
description
两个表之间的关系是多对一的(许多 comm 对一个 persondesc ):
Relationship between the two tables is many to one (many comm to one persondesc):
com.code = persondesc.code
这两个表都映射有注释,但是我没有声明任何关系.
These two tables are mapped with annotations but I have no relation declared.
我要尝试的是选择persondesc.description
排序的comm
表.
What I'm trying to is to select comm
table ordered by persondesc.description
.
我该如何执行JPA和Hibernate?
How can I do this JPA and Hibernate?
推荐答案
因此,如果您的类没有关系",则可以执行类似的查询
So if your classes have no "relation" then you do a query like
SELECT a FROM A a, B b WHERE a.someField = b.otherField ORDER BY b.anotherField
使用JPA标准可以实现哪些目标,例如
Which can be achieved using JPA Criteria, something like
CriteriaBuilder cb = emf.getCriteriaBuilder();
CriteriaQuery<A> crit = cb.createQuery(A.class);
Root<A> aRoot = crit.from(A.class);
Root<B> bRoot = crit.from(B.class);
aRoot.alias("a");
bRoot.alias("b");
crit.select(aRoot);
Predicate fieldEquals = cb.equal(aRoot.get(A_.someField), bRoot.get(B_.otherField);
crit.where(fieldEquals);
crit.orderBy(cb.asc(bRoot.get(B_.anotherField)));
...或者只是重新设计您的类,对您的开发人员有所帮助.
... Or just redesign your classes and do your developers a favour.
这篇关于使用JPA标准在没有关系的情况下联接表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!