Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jpa 在查询中强制转换实体_Jpa_Jpql - Fatal编程技术网

Jpa 在查询中强制转换实体

Jpa 在查询中强制转换实体,jpa,jpql,Jpa,Jpql,我正在寻找一种在jpql查询中强制转换实体的方法 例如: @Entity class Topic { @OneToMany List<AbstractMessage> messages; } @Entity @Inheritance(strategy = InheritanceType.JOINED) abstract class AbstractMessage { String content; } @Entity class MessageType1 exten

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

例如:

@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;
}
我认为这个查询不起作用,因为JPA没有加入MessageType2表

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

谢谢

您可以模拟投射:


清洁剂的解决方案是使用JPA 2.1
TREAT

SELECT t FROM Topic t JOIN TREAT(t.messages AS MessageType2) m WHERE m.att2 = 1

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