我有两个表:Tax
和TaxRule
。两个表中都有一列相同,即TAX_RULE_ID
。但是没有像OneToOne
或OneToMany
这样的任何Hibernate映射。这两个表看起来像 -
TAX
@Id
@Column(name = "TAX_RATE_ID")
private Long taxRateId;
@Column(name = "TAX_RULE_ID")
private Long taxRuleId;
@Column(name = "TAX_TYPE")
private String taxType;
TAX_RULE
@Id
@Column(name = "TAX_RULE_ID")
private Long taxRuleId;
@Column(name = "TAX_CD")
private String TaxCode;
@Column(name = "STATE")
private String state;
我正在尝试获取密钥的数据,即TAX_RULE_ID
。此列在两个表中都很常见。
我有以下Hibernate
代码,我在TAX_RULE_ID
列中加入了两个表格,如下所示:
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<String[]> cQuery = criteriaBuilder.createQuery(String[].class);
Root<Tax> taxRoot = cQuery.from(Tax.class);
cQuery.multiselect(taxRateRoot.get("taxType"), taxRateRoot.get("taxRate"));
List<Predicate> predicates = new ArrayList<>();
Join<Tax, TaxRule> join = taxRoot.join("taxRuleId");
.....rest of the code.
我在连接点上得到以下异常:
org.hibernate.jpa.criteria.BasicPathUsageException: Cannot join to attribute of basic type
at org.hibernate.jpa.criteria.path.AbstractFromImpl.constructJoin(AbstractFromImpl.java:270)
at org.hibernate.jpa.criteria.path.AbstractFromImpl.join(AbstractFromImpl.java:263)
at org.hibernate.jpa.criteria.path.AbstractFromImpl.join(AbstractFromImpl.java:436)
at com.iclsystems.base.dao.TaxRateDAOImpl.getTaxTypeForApplicableWorkOrderTax(TaxRateDAOImpl.java:31)
at com.iclsystems.base.businessObjects.TaxLookupBOImpl.getTaxTypeForApplicableWorkOrderTax(TaxLookupBOImpl.java:53)
at com.iclsystems.base.businessObjects.TaxLookupBOImpl.getWorkOrderTaxLookup(TaxLookupBOImpl.java:29)
at com.iclsystems.test.eventhandler.base.TaxLookupBOTest.testTaxLookupBO(TaxLookupBOTest.java:52)
答案 0 :(得分:8)
您无法加入@Column
。加入是为了协会:
您需要删除此行,因为已从数据库中提取taxRuleId
:
Join<Tax, TaxRule> join = taxRoot.join("taxRuleId");
如果您想加入TaxRule表,则需要替换:
@Column(name = "TAX_RULE_ID")
private Long taxRuleId;
与多对一关联:
@ManyToOne
@JoinColumn(name = "TAX_RULE_ID")
private TaxRule raxRule;