0
我有一个包含AccountOwner表和Account表的数据库。账户表具有PK“account_number”,并且AccountOwner表具有与FK相同的属性。我想加盟此属性的两个表,这里是我的代码:条件API - 连接两个表会抛出异常
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
[[email protected]: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'。 – ujulu
谢谢,那是问题所在。 – user129186