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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30166806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 02:51:31  来源:igfitidea点击:

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

javaoraclehibernatejpahibernate-criteria

提问by Avinash

I have two tables: Taxand TaxRule. There is one column same in both table i.e TAX_RULE_ID. But don't have any Hibernate mapping like OneToOneor OneToMany. Both table looks like-

我有两个表:TaxTaxRule。两个表中有一列相同,即TAX_RULE_ID. 但是没有像OneToOne或那样的任何 Hibernate 映射OneToMany。两张桌子看起来都像-

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

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 TAX_RULE_ID. This column is common in both table. I have following Hibernatecode in which I am joining both table on the TAX_RULE_IDcolumn as follows:

我正在尝试获取密钥 ie 上的数据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.

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)

回答by Vlad Mihalcea

You cannot join a basic property (e.g., an attribute with a simple @Columnmapping). Join is for associations:

您不能加入基本属性(例如,具有简单@Column映射的属性)。加入是为协会:

  • one-to-one
  • one-to-many
  • many-to-one
  • many-to-many
  • 一对一
  • 一对多
  • 多对一
  • 多对多

You need to remove this line, as the taxRuleIdis 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:

如果要加入TaxRule表,需要替换:

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

with a many-to-one association:

多对一关联:

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