【发布时间】:2016-05-14 17:55:20
【问题描述】:
我有一个包含 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)
//....
}
我做错了什么?
【问题讨论】:
-
在
aroot.join("account_number");中使用accountNumber而不是account_number。 -
谢谢,这就是问题所在。
标签: java database join criteria-api