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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.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
Java JPA CriteriaBuilder子查询多选_Java_Jpa_Criteria_Criteria Api - Fatal编程技术网

Java JPA CriteriaBuilder子查询多选

Java JPA CriteriaBuilder子查询多选,java,jpa,criteria,criteria-api,Java,Jpa,Criteria,Criteria Api,我有一个关于jpa中的子查询类的问题。 我需要用两个自定义字段创建子查询,但子查询没有multiselect方法,select方法有表达式输入参数(在查询中这是Selection)和constact方法不适用 我还有关于联接子查询结果的问题,有可能吗?那怎么做呢 我有: 连锁性 公共类链{ @身份证 @列(name=“chain\u id”) @GeneratedValue(generator=“seq_cha_id”,strategy=GenerationType.SEQUENCE) @Seq

我有一个关于jpa中的子查询类的问题。 我需要用两个自定义字段创建子查询,但子查询没有multiselect方法,select方法有表达式输入参数(在查询中这是Selection)和constact方法不适用

我还有关于联接子查询结果的问题,有可能吗?那怎么做呢

我有:

连锁性

公共类链{
@身份证
@列(name=“chain\u id”)
@GeneratedValue(generator=“seq_cha_id”,strategy=GenerationType.SEQUENCE)
@SequenceGenerator(name=“seq\u cha\u id”,sequenceName=“seq\u cha\u id”,allocationSize=1)
私人长id;
@列(name=“user\u id”)
私有长用户ID;
@列(name=“operator\u id”)
私人长运营商;
@列(name=“subject”)
私有字符串主题;
@OneToMany(fetch=FetchType.LAZY,mappedBy=“chain”)
私人列表消息;
@列(name=“status”)
私人身份;
公共长getOperatorId(){
返回运算符ID;
}
公共无效setOperatorId(长运算符ID){
this.operatorId=operatorId;
}
公共状态getStatus(){
返回状态;
}
公共无效设置状态(状态){
这个状态=状态;
}
公共长getUserId(){
返回用户标识;
}
public void setUserId(长userId){
this.userId=userId;
}
公共长getId(){
返回id;
}
公共无效集合id(长id){
this.id=id;
}
公共字符串getSubject(){
返回主题;
}
公共void setSubject(字符串主题){
这个主题=主题;
}
公共列表getMessages(){
返回消息;
}
公共消息(列出消息){
this.messages=消息;
}
} 
信息完整性

公共类消息{
@身份证
@列(name=“message\u id”)
@GeneratedValue(generator=“seq\u mess\u id”,strategy=GenerationType.SEQUENCE)
@SequenceGenerator(name=“seq\u mess\u id”,sequenceName=“seq\u mess\u id”,allocationSize=1)
私人长id;
@列(name=“user\u id”)
私有长用户ID;
@列(name=“message”,nullable=true,长度=4000)
私有字符串消息;
@manytone(fetch=FetchType.LAZY)
@JoinColumn(name=“chain\u id”)
私有链;
@列(name=“创建日期”)
私人日期;
@列(name=“status”)
私人身份;
公共长getId(){
返回id;
}
公共无效集合id(长id){
this.id=id;
}
公共长getUserId(){
返回用户标识;
}
public void setUserId(长userId){
this.userId=userId;
}
公共字符串getMessage(){
返回消息;
}
公共无效设置消息(字符串消息){
this.message=消息;
}
公共链getChain(){
返回链;
}
公共无效设置链(链链){
这个链子=链子;
}
公共日期getDate(){
返回日期;
}
公共作废设置日期(日期){
this.date=日期;
}
公共状态getStatus(){
返回状态;
}
公共无效设置状态(状态){
这个状态=状态;
} 
}
查询包装器

公共类MessageWrapper{
私有最终长链ID;
私有最终长消息ID;
公共消息包装器(长链ID、长消息ID){
this.chainId=chainId;
this.messageId=messageId;
}
}
我需要创建这个查询(这是查询的一部分,我从predicates.JPQL获得的另一部分不适用)

选择通道*
从hl_chain ch,
(选择mes.chain\U id,
最大(消息id)消息id
从hl_消息mes
按链分组(id)mes
其中mes.chain\u id=ch.chain\u id
按消息_id排序;
在子查询中,我是这样做的

Subquery Subquery=criteriaQuery.Subquery(MessageWrapper.class);
Root subRoot=subquery.from(Message.class);
subquery.select(cb.construct(
MessageWrapper.class,
subRoot.get(消息链),
cb.max(subRoot.get(Message.id))
));

但是,子查询在params中没有select with CompoundSelection,我不能使用
CriteriaBuilder
构造方法。

您可以从JPA调用本机查询,例如:

Query q = em.createNativeQuery("SELECT p.firstname, p.lastname FROM Person p");
List<Object[]> persons= q.getResultList();

for (Object[] p : persons) {
    System.out.println("Person "
            + p[0]
            + " "
            + p[1]);
}
Query q=em.createNativeQuery(“从Person p中选择p.firstname、p.lastname”);
List persons=q.getResultList();
(对象[]p:人){
System.out.println(“个人”
+p[0]
+ " "
+p[1]);
}
CriteriaBuilder cb=session.getCriteriaBuilder();
CriteriaQuery q=cb.createQuery(MessageWrapper.class);
根c=q.from(链类);
Join m=p.Join(“消息”);
q、 groupBy(c.get(“id”));
q、 选择(cb.construct(MessageWrapper.class、c.get(“id”)、cb.max(m.get(“id”)));

映射为实体的数据库视图将完成您需要的工作。 它被映射为一个普通表,只使用标记@View


我在我的项目上也做了同样的工作。

如果没有具体的数据示例、要执行的查询类型和代码片段,几乎不可能为您提供帮助。但似乎你根本不需要子查询,但可能需要加入。我同意,请发布一些代码。你有什么想法吗?我在这里,想知道2016年12月的同样事情。在何处用子查询替换加入不是一个选项吗?避免只包含代码的回复。添加解释。
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<MessageWrapper> q = cb.createQuery(MessageWrapper.class);
Root<Chain> c = q.from(Chain.class);
Join<Chain, Message> m = p.join("messages");
q.groupBy(c.get("id"));
q.select(cb.construct(MessageWrapper.class, c.get("id"), cb.max(m.get("id"))));