有没有更简洁的方法在春季JPA中构建MappedSuperclass Tree类?



我目前有几个实体表现为树,需要将它们保存到数据库中。

因此,为了不为此编写重复的代码,我构建了这个类:

@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) -> 对 setParent(T) 作为原始类型 TreeStructure 成员的未经检查的调用

pParent.children.add(this) -> 未选中的调用 add(E) 作为原始类型 java.util.Set 的成员

我也尝试不使用泛型,所以我可以只使用抽象的树结构,然后从其他类扩展,但随后我在父/子类方面遇到了问题,因为您无法从 OneToMany/ManyToOne 引用引用 MappedSuperclass。

所以,最后进入正题:是否有任何以更好/更清洁的方式实现这一点?这个警告是有意义的,还是只是Intellij不够聪明?

问题不在于 JPA,而在于泛型的使用。

首先,更改抽象类签名,使其具有递归类型:

public abstract class TreeStructure<T extends TreeStructure<T>>

接下来,你不能引用"this",因为你不知道"this"的实现,所以你可以把它转换为'T',或者添加一个带有签名的抽象方法,如下所示:

public abstract T getImpl();

在实现中,只需返回"this"。

public T getImpl() {
return this;
}

在侧节点上,访问类中的父类实例变量可能不是一个好主意。将addChild和removeChild方法添加到TreeStructure类中可能是一个更好的主意。

我有一个非常相似的场景,我没有使用T。相反,我只有抽象类,因为我不需要类型子级的灵活性,而且我没有强制转换。据我所知,它可能会让您接地(共享代码),但我不知道您是否还有其他要求。

在我的情况下,另一个区别是抽象类不是映射的超类,而是@Inheritance(strategy = InheritanceType.SINGLE_TABLE)

如果有帮助,您可以在此存储库中找到完整的工作示例

@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class TreeStructure {
...
@ManyToOne(cascade = CascadeType.PERSIST)
private TreeStructure  parent;
@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
protected Set<TreeStructure> children = new HashSet<>();

相关内容

  • 没有找到相关文章

最新更新