我有一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
- 是带有某些货币的 Enum。现在我正在尝试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()
,它返回两个重复项。有人知道发生了什么吗?