【问题标题】:JPA Cannot join to attribute of basic typeJPA无法加入基本类型的属性
【发布时间】:2015-06-24 12:44:55
【问题描述】:
    log.debug("get category list start "+id);       
    List<CategoryResponseView> usersViewList = new ArrayList<CategoryResponseView>();
    long a=System.currentTimeMillis(); 

    CriteriaBuilder builder = em.getCriteriaBuilder();
    Metamodel  metamodel=em.getMetamodel();
    EntityType EntrepreneurCategory_=metamodel.entity(EntrepreneurCategory.class);
    CriteriaQuery<Object[]> query = builder.createQuery(Object[].class);

    Root<EntrepreneurCategory> entrepreneurCategoryRoot=query.from(EntrepreneurCategory.class);


    Join<EntrepreneurCategory, Category> categoryJoin = entrepreneurCategoryRoot.join("category_id",JoinType.LEFT);
    Join<EntrepreneurCategory, Cluster> clusterJoin = entrepreneurCategoryRoot.join("cluster_id",JoinType.LEFT);

    List<Predicate> conditions = new ArrayList();
    conditions.add(builder.equal(entrepreneurCategoryRoot.get("id"), id));

    TypedQuery<Object[]> typedQuery = em.createQuery(query
            .multiselect(entrepreneurCategoryRoot).where(conditions.toArray(new Predicate[] {}))                          
    );

    return usersViewList;

org.hibernate.ejb.criteria.BasicPathUsageException:无法加入 基本类型的属性在 org.hibernate.ejb.criteria.path.Abs​​tractFromImpl.constructJoin(AbstractFromImpl.java:262) 在 org.hibernate.ejb.criteria.path.Abs​​tractFromImpl.join(AbstractFromImpl.java:255) 在 org.hibernate.ejb.criteria.path.Abs​​tractFromImpl.join(AbstractFromImpl.java:428) 在 com.vs.manifesto.logic.EntrepreneurCategoryBDL.getCategoryList(EntrepreneurCategoryBDL.java:55) 在 com.vs.manifesto.scoreboard.services.CategoryServices.getCategoryList(CategoryServices.java:44)

以下是模型类元素

@ManyToOne
@JoinColumn(name = "category_id",referencedColumnName="id",insertable=false,updatable=false)
private Category  category;

private String category_id;

public Category getCategory() {
    return category;
}

public void setCategory(Category category) {
    this.category = category;
}

如何解决这个问题..?

【问题讨论】:

  • 在 O-O 的世界里,我们不加入基本属性。你有关系并加入这些关系。
  • @NeilStockton 这里我有 category_id;对类别表 id 字段的外键引用。我需要指定什么,以便将 category_id 用作关系并加入
  • 您加入“类别”而不是 category_id ......它们等同于同一列。

标签: hibernate jpa hibernate-criteria


【解决方案1】:

你加入了基本类型的属性,因为你加入了 String "category_id"

"Join<EntrepreneurCategory, Category> categoryJoin = entrepreneurCategoryRoot.join("category_id",JoinType.LEFT);"

您需要更改才能加入以下类别:

"Join<EntrepreneurCategory, Category> categoryJoin = entrepreneurCategoryRoot.join("category",JoinType.LEFT);"

【讨论】:

    猜你喜欢
    • 2015-07-21
    • 1970-01-01
    • 2014-01-30
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 2019-03-22
    • 2018-07-12
    相关资源
    最近更新 更多