【问题标题】:Unable to locate Attribute with the the given name [XXX] on this ManagedType [unknown]无法在此 ManagedType [未知] 上找到具有给定名称 [XXX] 的属性
【发布时间】:2019-05-21 10:37:54
【问题描述】:

我有这个实体,其 ID 在 Identifiable 类中定义。

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
}

我正在使用 Criteria builder 来获取 ID 类的列。

为了得到这个,使用Path来获取Inventoryid的路径,即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]

【问题讨论】:

  • 你肯定想念@Entity
  • 我刚刚粘贴了代码的sn-ps。存在实体标签。

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


【解决方案1】:

hibernate 中的标准可以与 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"));

【讨论】:

  • 感谢您的回复。但我收到编译错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多