gpt4 book ai didi

java - 在 Spring JPA 中是否有更简洁的方式构建 MappedSuperclass Tree 类?

转载 作者:行者123 更新时间:2023-12-02 10:04:37 28 4
gpt4 key购买 nike

我目前有几个充当树的实体,需要将它们保存到数据库中。

所以为了不重复代码,我构建了这个类:

@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" -> Cannot resolve attribute parent.

children.iterator().next().setParent(parent) -> Unchecked call to setParent(T) as a member of raw type TreeStructure

pParent.children.add(this) -> Unchecked call to add(E) as a member of raw type java.util.Set

我也尝试不使用泛型,所以我可以只使用抽象 TreeStructure,然后从其他类扩展,但随后我遇到了父/子类问题,因为您无法从 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;
}

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

关于java - 在 Spring JPA 中是否有更简洁的方式构建 MappedSuperclass Tree 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55400884/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com