hibernate.jpa.criteria.BasicPathUsageException:无法加入基本类型的属性

时间:2015-05-11 11:44:32

标签: java oracle hibernate jpa hibernate-criteria

我有两个表:TaxTaxRule。两个表中都有一列相同,即TAX_RULE_ID。但是没有像OneToOneOneToMany这样的任何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)

1 个答案:

答案 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;