在我的项目中,我试图用其他库中的实体替换现有实体。我在规范,标准制定者和加入内容方面遇到了一个奇怪的问题。我在这里有以下课程
@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));
}
specifications UT IL.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是一个对象,而新的ResEntity中只有RoomId。当我使用新类型的ResEntity时,我遇到了各种各样的错误。
我收到错误
Join<RoomEntity, RoomHierarchyEntity> chRoot = root.join("room").join("children");
Unable to locate Attribute with the the given name [children] on this ManagedType
如何使用RoomEntity和RoomHierarchy实体加入传入实体(ResEntity)?
上面的代码是遗留代码,我真的不明白它的作用。我只是想加入这些表格,我想在那些令人不安的事情上运行。
你改变了实体,现在没有字段room
,children
。现在你不能使用root.join("room")
因为它不存在关系。如果要加入它们(在一个查询中),请在ResourceEntity中还原我们的更改。