在查询中强制转换实体

编程入门 行业动态 更新时间:2024-10-28 02:22:25
本文介绍了在查询中强制转换实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在寻找一种在jpql查询中强制转换实体的方法.

I'm looking for a way to cast an entity inside a jpql query.

示例:

@Entity class Topic { @OneToMany List<AbstractMessage> messages; } @Entity @Inheritance(strategy = InheritanceType.JOINED) abstract class AbstractMessage { String content; } @Entity class MessageType1 extends AbstractMessage { String att1; } @Entity class MessageType2 extends AbstractMessage { Integer att2; }

我正在尝试收集所有一个或多个消息的类型为MessageType2且att2 = 1的所有Topic.

I'm trying to collect all Topic where one or more of its messages have the type MessageType2 and have att2 = 1.

以下是我对jpql查询的建议:

Here is my suggestion as a jpql query:

select t from Topic t left outer join t.messages on (Type(t) = MessageType2) where t.att2 = 1

我不认为此查询有效,因为JPA没有加入MessageType2表.

I don't think this query works because JPA doesn't join the MessageType2 table.

有没有办法做到这一点,或者我必须进行本地查询?

Is there a way to do that or I have to make a native query?

谢谢!

推荐答案

您可以模拟CAST: en.wikibooks/wiki/Java_Persistence/Querying#Joining.2C_querying_on_a_OneToMany_lationship

You can simulate the CAST: en.wikibooks/wiki/Java_Persistence/Querying#Joining.2C_querying_on_a_OneToMany_relationship

SELECT t FROM Topic t JOIN t.messages m, MessageType2 y WHERE m = y AND y.att2 = 1

更多推荐

在查询中强制转换实体

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

发布评论

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

>www.elefans.com

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