JPA MappedSuperclass with ManyToOne

zdenda.online picture zdenda.online · Jan 7, 2012 · Viewed 7k times · Source

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.

Answer

zdenda.online picture zdenda.online · Apr 10, 2012

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