2

i want define a hibernate PersistentSet for scala mutable Set, but i have this compile error :-(

class MySet extends org.hibernate.collection.internal.AbstractPersistentCollection with collection.mutable.Set[Any] {
  ...
}

error:

incompatible type in overriding
def empty(): Boolean (defined in class AbstractPersistentCollection);
 found   : scala.collection.mutable.Set[Any]
 required: (): Boolean
  override def empty: mutable.Set[Any] = ???

if there's a good way to fix it.

Golden
  • 33
  • 4

1 Answers1

1

The only thing that would work in theory is defining def empty() in MySet with return type mutable.Set[Any] with Boolean. That should compile but it is impossible to find a reasonable implementation.

Alternatively you could define conversions between mutable.Set[Any] and AbstractPersistentCollection instead of using inheritance. You could do something like this:

class MySet private (private val set: mutable.Set[Any]) extends AbstractPersistentCollection {
  override def empty(): Boolean = set.isEmpty

  ...
}

object MySet {
  def empty(): MySet = new MySet(mutable.Set.empty[Any])
  def convert(set: mutable.Set[Any]): MySet = new MySet(set)
  def convert(set: MySet): mutable.Set[Any] = set.set
}
Jasper-M
  • 14,966
  • 2
  • 26
  • 37
  • Thanks. but i can't use alternative solution, because i need `MySet` implements `mutable.Set[Any]` using this collection type in my entity `class MyEntity { val children: mutable.Set[ChildEntity] = new mutable.HashSet[ChildEntity] ...` and hibernate xml mapping `... ...` – Golden Nov 05 '21 at 15:18