【问题标题】:Unable to locate Attribute with the the given name无法找到具有给定名称的属性
【发布时间】:2019-03-18 18:43:30
【问题描述】:

在我的项目中,我试图用其他库中的实体替换现有实体。我在规范、标准构建器和连接方面遇到了一个奇怪的问题。这里我有以下课程

    @Entity
@Table(
    name = "company"
)
public class RoomEntity{
    @Id
    @Column(
        name = "id"
    )
    @GeneratedValue(
        generator = "seq-table",
        strategy = GenerationType.TABLE
    )
    private Integer id;

    @OneToMany(
    mappedBy = "parentCompany",
    fetch = FetchType.LAZY,
    orphanRemoval = false
)
private Set<RoomHierarchyEntity> children;

2

@Entity
@Table(
    name = "room_hierarchy"
)
public class RoomHierarchyEntity {
    @Id
    @Column(
        name = "id"
    )
    @GeneratedValue(
        generator = "seq-table",
        strategy = GenerationType.TABLE
    )
    private Integer id;
    @ManyToOne(
        fetch = FetchType.LAZY,
        optional = false
    )
    @JoinColumn(
        name = "parent_id",
        foreignKey = @ForeignKey(
    name = "fk_Roomhierarchy_p_room"
)
    )
    private RoomEntity parentRoom;
    @ManyToOne(
        fetch = FetchType.LAZY,
        optional = false
    )
    @JoinColumn(
        name = "child_id",
        foreignKey = @ForeignKey(
    name = "fk_Roomhierarchy_c_room"
)
    )
    private RoomEntity childRoom;
    @Column(
        name = "distance",
        nullable = false
    )
    private Integer distance;

3

public class ResourceEntity {
    @Id
    @Column(
        name = "id"
    )
    @GeneratedValue(
        generator = "seq-table",
        strategy = GenerationType.TABLE
    )
    private Long id;
    @Column(
        name = "room_id",
        nullable = false
    )
    @NotNull
    private Integer RoomId;

)

Service1.java

findByCompanyAnd(ResEntity.class, roomId);

Service2.java

public static <T> Specifications<T> findByRoomAnd(Class<T> queryClass, Integer companyId,
                                                                            ) {
    return findByCompany(queryClass, companyId));

}

SpecificationsUtil.Java

public static <T> Specifications<T> findByCompany(Class<T> queryClass, Integer companyId) {
    return findByCompany(queryClass, companyId, COLUMNS.get(queryClass));
}


private static <T> Specifications<T> findByRoom(final Class<T> queryClass, final Integer RoomId,
                                                                             final Set<String> columnNames) {
       return Specifications.where(new Specification<T>() {
           @Override
           public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
               Join<RoomEntity, RoomHierarchyEntity> chRoot = root.join("room").join("children");

               Subquery<Integer> sq = query.subquery(Integer.class);
               Root<T> sqRoot = sq.from(queryClass);
               Join<CompanyEntity, CompanyHierarchyEntity> sqChRoot = sqRoot.join("room").join("children");
               sq.select(sqChRoot.<Integer>get("distance"));

               ...
           }
       });
   }

旧的 ResEntity 和新的 ResEntity 的区别在于,旧的有实体 RoomEntity 作为对象,而新的只有 RoomId。放置新类型的 ResEntity 时出现各种错误。

我在

处遇到错误
Join<RoomEntity, RoomHierarchyEntity> chRoot = root.join("room").join("children");


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

如何将传入实体 (ResEntity) 与 RoomEntity 和 RoomHierarchy 实体连接起来?

上面的代码是遗留代码,我真的不明白它的作用。我只是想加入这些表,并且我想在没有干扰的情况下运行这些表。

【问题讨论】:

    标签: java hibernate jpa criteria specifications


    【解决方案1】:

    您更改了实体,现在没有字段 roomchildren。 现在你不能使用root.join("room"),因为它的关系不存在。如果您想加入它们(在一个查询中),请还原我们在 ResourceEntity 中的更改。

    【讨论】:

      猜你喜欢
      • 2018-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多