我正在寻找一种在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更多推荐
在查询中强制转换实体
发布评论