问题描述
我想在JPA中进行以下查询:
SELECT * FROM `happyDB`.`users` U WHERE U.party_as_user =1 AND U.party_party_id =2
这很好,但是我的问题是我只将Party
作为对象,而没有作为id,并且我无法使其工作.
在我要执行命名查询的Users
实体中,我具有以下内容:
@JoinColumn(name = "pary_party_id", referencedColumnName = "party_id")
@ManyToOne
private Party partyId;
@Column(name = "party_as_user")
private Boolean partyAsUser;
我试图像带点符号的对象一样实现它,但这不起作用:
@NamedQuery(name = "Users.findByPartyAsUser", query = "SELECT u FROM Users u WHERE u.partyAsUser = :partyAsUser AND u.partyId.partyId = :partyId")
在Party
对象中有一个名为partyId
的属性,但是它不起作用.对此有什么解决方案吗?还是每次向Users
中插入新的Party
时,都必须向Users-bean添加一个属性,如private int partyID
并填充它?
感谢您的帮助!萨米·努尔米(Sami Nurmi)
通常,您可以在JPA中将对象用作参数,
SELECT u FROM Users u WHERE u.partyAsUser = :partyAsUser AND u.party = :party
Party party = new Party(id);
query.setParameter("party", party);
但是,如果您使用正确的变量名,那么您应该可以使用
SELECT u FROM Users u WHERE u.partyAsUser = :partyAsUser AND u.party.id = :id
如果您比对象更了解SQL,那么您始终可以使用本机SQL查询.
I would like to make this query in JPA:
SELECT * FROM `happyDB`.`users` U WHERE U.party_as_user =1 AND U.party_party_id =2
This is working fine, but my problem is that I have Party
only as an object, not as an id and I can't make it work.
In the Users
-entity where I am trying to do the named query I have the following:
@JoinColumn(name = "pary_party_id", referencedColumnName = "party_id")
@ManyToOne
private Party partyId;
@Column(name = "party_as_user")
private Boolean partyAsUser;
I tried to implement it like an object with dot notation, but that's not working:
@NamedQuery(name = "Users.findByPartyAsUser", query = "SELECT u FROM Users u WHERE u.partyAsUser = :partyAsUser AND u.partyId.partyId = :partyId")
There is a property called partyId
inside the Party
-object, but it's not working. Is there any solution for that or do I have to add one property to the Users-bean like private int partyID
and populate it every time when a new Party
is inserted into Users
?
Thanks for helping!Sami Nurmi
In general you can use an object as a parameter in JPA,
SELECT u FROM Users u WHERE u.partyAsUser = :partyAsUser AND u.party = :party
Party party = new Party(id);
query.setParameter("party", party);
But what you have should work if you use the correct variable names, my guess is,
SELECT u FROM Users u WHERE u.partyAsUser = :partyAsUser AND u.party.id = :id
And you can always use a native SQL query, if you understand SQL better than objects.
这篇关于属性是对象时的命名查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!