[英]Criteria API - joining two tables throws an exception
我有一個數據庫,其中包含AccountOwner表和Account表。 Account表具有PK“ account_number”,而AccountOwner表具有與FK相同的屬性。 我試圖將兩個表聯接到此屬性上,這是我的代碼:
EntityManager em = emfactory.createEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Account> cq = cb.createQuery(Account.class);
Root<AccountOwner> aroot = cq.from(AccountOwner.class);
Join<AccountOwner,Account> ajoin = aroot.join("account_number");
cq.select(ajoin).where(cb.equal(aroot.get("ClientId"),k.getClientID()));
但是,這引發以下異常:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException:The attribute [account_number] is not present in the managed type
[EntityTypeImpl@18830730:AccountOwner[ javaType: class banking.AccountOwner descriptor: RelationalDescriptor(banking.AccountOwner--> [DatabaseTable(acccount_owner)]), mappings: 3]].
at org.eclipse.persistence.internal.jpa.metamodel.ManagedTypeImpl.getAttribute(ManagedTypeImpl.java:148)
我知道一個事實,即兩個表中都精確地調用了此屬性,這是實體類的相關部分:
public class AccountOwner implements Serializable {
//...
@JoinColumn(name = "account_number", referencedColumnName = "account_number")
@ManyToOne(optional = false)
private Account accountNumber;
//...
}
public class Account implements Serializable {
//....
@Column(name = "account_number")
private Integer accountNumber;
@Basic(optional = false)
//....
}
我究竟做錯了什么?
正如評論所說,我不得不使用accountNumber
而不是account_number
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.