I currently have several Entities that behave as a Tree and need to save them to the DB.
So in order to not have duplicated code for that I built this class:
@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());
}
}
Then to actually implement it I simply do:
@Entity(name = "CATEGORY")
public class Category extends TreeStructure<Category>
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonProperty("category_id")
private long id;
// etc...
As far as I know, everything is working like a charm but everytime I get inside the TreeStructure class Intellij highlights some errors:
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
I also tried not using generics so I could just have abstract TreeStructure and then extend from the other classes but then I have problems with parent/children since you can not refer a MappedSuperclass from OneToMany/ManyToOne references.
So, finally getting to the point: Is there anyway of implementing this in a better/cleaner way? Are this warnings meaningfull or it's just Intellij not being smarter enough?