从递归休眠查询中获取最小字符串

时间:2019-02-07 15:01:45

标签: hibernate hql recursive-datastructures

我有一个与自身具有ManyToMany关系的实体:

@Entity
@Table(name = "asset")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Asset implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Enumerated(EnumType.STRING)
@Column(name = "provided_c")
private EnumerationClass providedC; // A0, A1, A2, ...

@ManyToMany
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@JoinTable(name = "asset_dependencies",
    joinColumns = @JoinColumn(name = "assets_id",  `referencedColumnName = "id"),`
    inverseJoinColumns = @JoinColumn(name = "dependencies_id", referencedColumnName = "id"))
private Set<Asset> dependencies = new HashSet<>();

当我在Spring Data存储库中调用此查询时,我确实获得了所有资产和相关资产。

@Query(value = "select distinct asset from Asset supporting_asset left join fetch asset.dependencies")
List<Asset> findAllWithEagerRelationships();

但是我需要获取另一个计算字段。 所有资产都有一个分类(提供的C:A0 -A3)。计算所得的字段将是该资产所依赖的所有资产的最低类别。

是否有实现此目标的好方法? 还是正确的方法?

1 个答案:

答案 0 :(得分:0)

使用@Formula实现此功能。您可以放置​​一个评估最低相关类别的查询,该查询将在实体加载时触发。