Java JPA @NamedQuery 有两个表,可能吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1943899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPA @NamedQuery with two tables, possible?
提问by rafa.ferreira
I'm having a kind of dummy problem, I need to make a @NamedQuery
with a join with other table, some simple thing.
我有一种虚拟问题,我需要@NamedQuery
与其他表连接,一些简单的事情。
But in all my @NamedQuery
I'm only dealing with my mapped Object/Table.
但总的来说,我@NamedQuery
只处理映射的对象/表。
For example in my Object/Table Mapped cars:
例如在我的对象/表映射汽车中:
@NamedQuery(name = Cars.GET_AVAILABLE_CARS,
query = "select c.id from Cars c where c.status = (select d.status from CarStatus d where d.color=red)")
I'm trying to use: @SecondaryTables
but no success for now.
我正在尝试使用:@SecondaryTables
但目前没有成功。
One other thing that is working is give all things from other table as a parameter, but I don't think this will be good in performance.
另一件事是将其他表中的所有内容作为参数,但我认为这不会有很好的性能。
Like:
喜欢:
@NamedQuery(name = Cars.GET_AVAILABLE_CARS, query =
"select c.id from Cars c where c.status = :" + CarStatusParam)
Any tips?
有小费吗?
Thanks in advance
提前致谢
回答by DataNucleus
A named query can use everything that an API query can use, so can clearly do subqueries. Which implementation of JPA ?
命名查询可以使用 API 查询可以使用的所有内容,因此可以清楚地执行子查询。JPA 的哪个实现?
回答by Bozho
What you are trying to do is not a join. It is a subselect. And in terms of performance it is as (in)efficient as getting the param beforehand.
您要做的不是加入。它是一个子选择。就性能而言,它与事先获取参数一样(低)效率。
If you insist, however, to use the subselect, the JPA Query Language support it. As it supports joins.
Take a look here(at 7.118.11 . Subqueries).
但是,如果您坚持使用子选择,则 JPA 查询语言支持它。因为它支持连接。
看看这里(在7.118.11 . 子查询)。
回答by Frank Orellana
I guess that you have something like this:
我猜你有这样的事情:
@Entity
public class Cars{
private String status;
//... something else
}
@Entity
public class CarStatus{
private String status;
private String color;
//... something else
}
if so, change this
如果是这样,请更改此
@Entity
public class Cars{
private CarStatus status; //<--THIS!!
//... something else
}
@Entity
public class CarStatus{
private String color;
//... something else
}
and then update your NamedQuery to this:
然后将您的 NamedQuery 更新为:
query ="select c.id from Cars c where c.status.color = 'red'"
If I am wrong and the tables should not be related that way, then you should change your query tu use a join instead of a subquery. Something like this:
如果我错了并且表不应该以这种方式相关,那么您应该更改您的查询 tu 使用连接而不是子查询。像这样的东西:
query = "select c.id from Cars c join CarStatus d where c.status = d.status and d.color = 'red'"
回答by Joshua Raymond
The answer is yes it's possible. You need to make sure your columns define which table to look in. See the below code, you named query should work after that addition.
答案是肯定的。您需要确保您的列定义了要查找的表。请参阅下面的代码,您命名的查询应该在添加后工作。
@Column(table = "SECONDARY_TABLE", name = "EXAMPLE_COLUMN_NAME")
private String example;
@Column(table = "SECONDARY_TABLE", name = "EXAMPLE_COLUMN_NAME")
private String example;