在此ManagedType上找不到具有给定名称[XXX]的属性[未知]

时间:2019-05-21 10:37:54

标签: java hibernate criteria-api hibernate-5.x criteriaquery

我有这个实体,其ID在可识别类中定义。

InventoryLoad将InventoryLoadID作为其PK

public class InventoryLoad extends AbstractIdentifiable<InventoryLoadId> implements Auditable {

    @OneToMany(mappedBy = "inventoryLoad")
    private Set<InventorySubLoad> inventorySubLoads = Sets.newLinkedHashSet();

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumns({
            @JoinColumn(name = "stoloc", referencedColumnName = "stoloc"),
            @JoinColumn(name = "wh_id", referencedColumnName = "wh_id")
    })
    private Location location;

    @Column(name = "lodwgt")
    private Double loadWeight;

    @Column(name = "prmflg")
    private Boolean permanentLoadSubFlag;
}

这是以上课程的ID

@Embeddable
public class InventoryLoadId extends AbstractIdentifiableId {
    private static final long serialVersionUID = 1L;


    @Column(name = "lodnum")
    private String loadNumber;

    // some another code below
}

我正在使用“条件构建器”进入ID类列。

要使用Path来获取此信息,请获取清单编号为PK的路径。

Session session = new HibernateTools().getSession();

CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class);

Root<InventoryLoad> inventoryLoadRoot = criteriaQuery.from(InventoryLoad.class);
Path<InventoryLoadId> inventoryLoadIdPath = inventoryLoadRoot.get("id");
criteriaQuery.multiselect(inventoryLoadIdPath.get("loadNumber"),

遇到此错误

Unable to locate Attribute  with the the given name [loadNumber] on this ManagedType [unknown]

1 个答案:

答案 0 :(得分:0)

休眠条件适用于JPA静态元模型。尝试使用它

@Embeddable
public class InventoryLoadId extends AbstractIdentifiableId {
    private static final long serialVersionUID = 1L;

    @Column(name = "lodnum")
    private String loadNumber;

}

@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(InventoryLoadId.class)
public abstract class InventoryLoadId_ {

    public static volatile SingularAttribute<InventoryLoadId, String> loadNumber;

}

Session session = new HibernateTools().getSession();
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();

// create the query
CriteriaQuery<InventoryLoadId> query = criteriaBuilder.createQuery(InventoryLoadId.class);

// set the root class
Root<InventoryLoadId> inventoryLoadRoot = query.from(InventoryLoadId.class);

query.multiselect(inventoryLoadRoot.get(InventoryLoadId_.loadNumber).alias("loadNumber"));