在我的项目中,我试图用另一个库中的实体替换现有实体。我在规范,criteriabuilder和联接方面遇到了一个奇怪的问题。这里我有以下课程
@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之间的区别在于,旧的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实体一起加入?
以上代码是旧代码,我真的不明白它的作用。我只想加入这些表,并希望在不打扰的情况下运行它们。
答案 0 :(得分:0)
您更改了实体,现在没有字段room
,children
。
现在,您不能使用root.join("room")
,因为它不存在该关系。如果您想加入它们(在一个查询中),请还原我们对ResourceEntity的更改。