hibernate.jpa.criteria.BasicPathUsageException: Cannot join to attribute of basic type

I have two tables: and . There is one column same in both table i.e . But don't have any Hibernate mapping like or . Both table looks like-TaxTaxRuleTAX_RULE_IDOneToOneOneToMany

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;

I am trying to fetch data on the key i.e . This column is common in both table. I have following code in which I am joining both table on the column as follows:TAX_RULE_IDHibernateTAX_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.

I am getting following Exception on the join point:

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

You cannot use the annotation for a basic property (e.g., an attribute with a simple mapping). is for associations:@Join@Column@Join

  • one-to-one
  • one-to-many
  • many-to-one
  • many-to-many

You need to remove this line, as the is already fetched from the database:taxRuleId

Join<Tax, TaxRule> join = taxRoot.join("taxRuleId");

If you want to join the TaxRule table, you need to replace the:

@Column(name = "TAX_RULE_ID")
private Long taxRuleId;

with a many-to-one association:

@ManyToOne
@JoinColumn(name = "TAX_RULE_ID")
private TaxRule raxRule;

答案 2

推荐