我有课程:城市,建筑和连锁建筑
城市有建筑物集合
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "city_id", insertable = false, updatable = false)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
protected Set<Building> buildings;
@Entity
@DiscriminatorValue(value = "CB")
public class ChainBuilding extends Building {
public ChainBuilding() {
}
}
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorValue(value = "B")
public class Building {
public Building() {
}
}
我需要收集连锁建筑物并按以下方式进行:
public Set<ChainBuilding> getChainBuildings() {
Set<ChainBuilding> result = new HashSet<>();
for (Building item : buildings) {
if (item instanceof ChainBuilding) {
result.add((ChainBuilding) item);
}
}
return result;
}
Profiler说这个(java.util.Set.iterator)是我的应用程序的瓶颈。我怎样才能改进它? 可以添加另一个chainBuildings集合,如果出现新的chainbuilding,我将需要将此对象添加到两个集合中,但在这种情况下我收到错误,因为hibernate尝试将同一个对象插入到1个表中2次。有任何想法吗? TIA