使用JPA标准在没有关系的情况下联接表

编程入门 行业动态 更新时间:2024-10-24 08:30:06
本文介绍了使用JPA标准在没有关系的情况下联接表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有两个没有建模关系的表:

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标准在没有关系的情况下联接表

本文发布于:2023-10-28 18:28:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537424.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:情况下   关系   标准   JPA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!