Domanda

I'm facing kind of typical issue. Imagine typical 1-N relationship between objects. To be precise User(U) and Room(R): [U]*---1[R].

Here comes the problem, Room should be abstract base class with implementations for example BlueRoom, RedRoom. How correctly set relationship within User entity?

public interface Room { ... }
@MappedSuperclass
public abstract class RoomSuperclass implements Room { ... }
@Entity
public class BlueRoom extends RoomSuperclass { ... }
@Entity
public class RedRoom extends RoomSuperclass { ... }


@Entity
public class User {

  @ManyToOne(targetEntity = ???) // I don't know if it will be BlueRoom or RedRoom
  private Room room; // ManyToOne cannot be applied on mapped superclass
}

I know this could be probably solved by using @Entity on RoomSuperclass instead of @MappedSuperclass but I'm not sure if it is good solution and if there is any better one.

È stato utile?

Soluzione

According to commentaries under the post. Declaring superclass as @Entity is the solution.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top