我有一个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()
并返回两个重复项。有人知道发生了什么吗?
答案 0 :(得分:0)
我不完全确定为什么你的查询返回重复,但是你应该能够通过编写这样的查询来解决它:"SELECT DISTINCT c FROM <CC.class.getName()> c ..."
(类似于你在SQL中的操作)