2016-01-03 23 views
0

我建立了一个简单的Query返回通过的可能性列表搜索的@Embeddable领域的结果列表。查询是:JPA其中嵌入优化(休眠和PostgreSQL)

select d from data d where embedField in :embedValues 

它工作正常,但与Hibernate和PostgreSQL发行到SQL Server所产生的本地查询是这样的:

select * from data where 
    f1=$1 and f2=$2 
    or 
    f1=$3 and f2=$4 
    or 
    f1=$5 and f2=$6 

我本来期望在本地查询更高效,例如,

select * from data where (f1,f2) in (($1,$2),($3,$4),($5,$6)) 

我写了namedQuery因为我想更好的表现,但我不认为我会与正在执行的查询得到它。有没有更好的方式来更容易地获得更好的原生查询?

为了完整起见,我添加了一些说明性的类。

@Embeddable class Embed { 
    int f1; 
    int f2; 
    public Embed(int i, int j) {f1 = i; f2 = j;} 
} 

@Entity class Datum { 
    @Embedded Embed embedField; 
} 

class Test { 
    public static void main(String... args) { 
    new Test().run(); 
    } 
    public void run() { 
    EntityManger em = ... 
    List<Datum> data = em.createQuery("select d from Data d where embedField in $1") 
     .setParameter(1, Arrays.asList(new Embed(1,2),new Embed(3,4),new Embed(5,6))) 
     .getResultList(); 
    } 
} 

感谢您的任何帮助,赞赏。

回答

1

嗯,我在一个字符串,做了什么,我想,如上所述写了一本机查询,但它给了绝对没有性能优势。我可以很好地想象到,当SQL服务器将查询转换为执行计划时,它无论如何都是相同的结果,因此没有区别。所以,答案是,不,没有更好的方法来编写JQPL查询,这样可以。