[英]Hibernate query.uniqueResult() throws NonUniqueResultException, but the record is Unique
我有一個Client
類,它是在一個對許多與ClientCurrency
這是一個對一個與PaymentRule
。
@Entity(name = "Client")
public class Client{
...
@OneToMany(mappedBy = "client")
@Fetch(FetchMode.SELECT)
@Cascade({CascadeType.ALL})
public Set<ClientCurrency> getClientCurrencies() {
return clientCurrencies;
}
}
@Entity(name = "Client_Currency")
public class ClientCurrency{
@ManyToOne
@JoinColumn(name = "client_id")
public Client getClient() {
return client;
}
@Column(name = "currency", length = 3)
@Enumerated(EnumType.STRING)
public Currency getCurrency() {
return currency;
}
}
Currency
-是帶有某些貨幣的枚舉。 現在,我嘗試使用此方法獲取ClientCurrency
:
public ClientCurrency get(Client client, Currency currency) {
StringBuilder hql = new StringBuilder().append("FROM ")
.append(ClientCurrency.class.getName())
.append(" WHERE client = :client AND currency = :currency");
Query query = getSessionFactory().getCurrentSession()
.createQuery(hql.toString()).setParameter("client", client)
.setParameter("currency", currency);
return (ClientCurrency)query.uniqueResult();
}
我知道該記錄在數據庫中是唯一的。 我嘗試使用query.list()
並返回兩個重復項。 有人知道發生了什么嗎?
我不完全確定為什么查詢會返回重復項,但是您應該能夠通過這樣編寫查詢來解決此問題: "SELECT DISTINCT c FROM <CC.class.getName()> c ..."
(類似於您在SQL中的操作方式)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.