首页 \ 问答 \ 使用JPA查询部分实体(Query partial entities with JPA)

使用JPA查询部分实体(Query partial entities with JPA)

我有一个类似的JPA实体;

public class Inventory {
 private String productname;
 private String manufacturer;
 private Float retailprice;
 private Integer unitssold;
 private String ourcomment;
}

在大约2/5的查询中,我需要整个实体,但剩下的时间我对unitssoldourcomment 感兴趣。

进行查询并获得大型结果列表但只需要3/5的信息就像是浪费。 我想稍微优化一下,我有预感这可以使用继承来完成。

但是怎么样?


I have a JPA entity similar to;

public class Inventory {
 private String productname;
 private String manufacturer;
 private Float retailprice;
 private Integer unitssold;
 private String ourcomment;
}

In about 2 out of 5 queries I need the whole entity, but the rest of the time I am not interested in unitssold and ourcomment.

It feels like a waste to make a query and get a large resultlist but only needing 3/5 of the information. I would like to optimize this slightly, and I have a hunch this can be done using inheritence.

But how?


原文:https://stackoverflow.com/questions/1359801
更新时间:2023-06-13 14:06

最满意答案

与评论相反,您的问题与表达SFINAE无关。 这是沼泽标准的原始配方SFINAE:您正在检查int C::* 类型的格式良好。

在我们开始之前,你把它括起来是错误的。 您想将sizeof应用于test结果,而不是==

enum { YES = (sizeof(IsClass<T>::test<T>(0)) == 1) };
             //     ^                      ^

(另外, 摆脱void main() 。)

现在,MSVC 2013由于某种原因尝试进行演绎,如果您进行合格的调用,则忽略指定的模板参数:

    IsClass<T>::test<T>(0)
//  ^^^^^^^^^^^^

这显然是一个错误。 我不确定为什么它会这样表现,但是MSVC前端的大部分都是用胶带固定在一起的,所以我并不感到惊讶。

无论如何,您不需要合格的电话。 只需编写test<T>(0)并进行无条件调用:

enum { YES = (sizeof(test<T>(0)) == 1) };

适用于我的MSVC 2013。


Contrary to the comments, your problem has nothing to do with expression SFINAE. This is bog-standard original-recipe SFINAE: you are checking the well-formedness of the type int C::*.

Before we start, you are parenthesizing it wrong. You want to apply sizeof to the result of test, not ==:

enum { YES = (sizeof(IsClass<T>::test<T>(0)) == 1) };
             //     ^                      ^

(Also, get rid of void main().)

Now, MSVC 2013 for some reason tries to do deduction and ignores the specified template argument if you do a qualified call:

    IsClass<T>::test<T>(0)
//  ^^^^^^^^^^^^

which is clearly a bug. I'm not sure why it's behaving this way, but then large parts of MSVC's frontend are held together by duct tape, so I'm not really surprised.

You don't need a qualified call anyway. Just write test<T>(0) and do an unqualified call:

enum { YES = (sizeof(test<T>(0)) == 1) };

which works on my MSVC 2013.

相关问答

更多