【发布时间】:2019-03-28 15:04:27
【问题描述】:
我目前有几个实体,它们的行为类似于树,需要将它们保存到数据库中。
所以为了避免重复代码,我构建了这个类:
@MappedSuperclass
public abstract class TreeStructure<T extends TreeStructure>
{
@ManyToOne(cascade = CascadeType.PERSIST)
private T parent;
@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
protected Set<T> children = new HashSet<>();
/**
* Function that is used before deleting this entity. It joins this.children to this.parent and viceversa.
*/
@Transactional
@PreRemove
public void preDelete()
{
unregisterInParentsChildren();
while (!children.isEmpty())
{
children.iterator().next().setParent(parent);
}
}
public abstract long getId();
protected void setParent(T pParent)
{
unregisterInParentsChildren();
parent = pParent;
registerInParentsChildren();
}
/**
* Register this TreeStructure in the child list of its parent if it's not null.
*/
private void registerInParentsChildren()
{
getParent().ifPresent((pParent) -> pParent.children.add(this));
}
/**
* Unregister this TreeStructure in the child list of its parent if it's not null.
*/
private void unregisterInParentsChildren()
{
getParent().ifPresent((pParent) -> pParent.children.remove(this));
}
/**
* Move this TreeStructure to an new parent TreeStructure.
*
* @param pNewParent the new parent
*/
public void move(final T pNewParent)
{
if (pNewParent == null)
{
throw new IllegalArgumentException("New Parent required");
}
if (!isProperMoveTarget(pNewParent) /* detect circles... */)
{
throw new IllegalArgumentException(String.format("Unable to move Object %1$s to new Object Parent %2$s", getId(), pNewParent.getId()));
}
setParent(pNewParent);
}
private boolean isProperMoveTarget(TreeStructure pParent)
{
if (pParent == null)
{
return true;
}
if (pParent == this)
{
return false;
}
return isProperMoveTarget(pParent.parent);
}
public int getLevel()
{
return getParent().map(pParent -> pParent.getLevel() + 1).orElse(1);
}
/**
* Return the <strong>unmodifiable</strong> children of this TreeStructure.
*
* @return the child nodes.
*/
public Set<T> getChildren()
{
return Collections.unmodifiableSet(this.children);
}
public Optional<T> getParent()
{
return Optional.ofNullable(parent);
}
public Optional<Long> getParentCategoryId()
{
return parent == null ? Optional.empty() : Optional.of(parent.getId());
}
}
然后要实际实现它,我只需这样做:
@Entity(name = "CATEGORY")
public class Category extends TreeStructure<Category>
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonProperty("category_id")
private long id;
// etc...
据我所知,一切都像一个魅力,但每次我进入 TreeStructure 类 Intellij 都会突出一些错误:
mappedBy = "parent" -> 无法解析父属性。
children.iterator().next().setParent(parent) -> 作为原始类型 TreeStructure 的成员对 setParent(T) 的未经检查的调用
pParent.children.add(this) -> 未经检查地调用 add(E) 作为原始类型 java.util.Set 的成员
我也尝试不使用泛型,所以我可以只使用抽象 TreeStructure,然后从其他类扩展,但是我遇到了父/子问题,因为您无法从 OneToMany/ManyToOne 引用中引用 MappedSuperclass。
所以,终于说到重点了:有没有办法以更好/更清洁的方式实现它?这个警告是有意义的还是只是 Intellij 不够聪明?
【问题讨论】:
-
可以忽略未检查的调用。
mappedBy = "parent"表示无论您使用什么T,它都可能没有名为parent的字段(我是这么理解的)
标签: java hibernate spring-boot intellij-idea spring-data-jpa