0

I have two classes Team and User :

@Entity
@Table(name = "team")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Team  extends AbstractAuditingEntity implements Serializable, Participant {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
    @SequenceGenerator(name = "sequenceGenerator")
    private Long id;

And

 @Entity
    @Table(name = "user")
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    @org.springframework.data.elasticsearch.annotations.Document(indexName = "user")
    public class User extends AbstractAuditingEntity implements Serializable, Participant {

        private static final long serialVersionUID = 1L;

        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
        @SequenceGenerator(name = "sequenceGenerator")
        private Long id;

I want to create new entity Match. Match can have 2 participants. Participant can be team or user. I want to create abstraction so i can add team or user to match as participant. I wanted to create Interface Participant and map it with @MappedSuperclass and then Team and User to implement Participant but i got error :

org.hibernate.AnnotationException: @OneToOne or @ManyToOne on Match.participantFirst references an unknown entity: Participant

@MappedSuperclass
public interface Participant {

}
user3364181
  • 531
  • 3
  • 14
  • 32
  • I cannot be certain without seeing your Match class but my first guess would be that as Participant as show has no Id value so the JPA does not have something to connect with – Dinomaster Oct 16 '18 at 10:44
  • Yes Participant is Interface so he has no ID. I cant create it as class since Team and User already extends something. How else can i solve this issue ? – user3364181 Oct 16 '18 at 10:51
  • 1
    You could try making an AbstractParticipantEntity which extends AbstractAuditingEntity contains the id and is in turn extended by User and team. – Dinomaster Oct 16 '18 at 11:02
  • Problem with this is that i already have id in team and user and if i remove it i dont know how it will behave since i have teams and users on production. – user3364181 Oct 16 '18 at 11:30
  • set up a test database with users and teams with id's and then run the change and see how it works. It should be find I have done similar things though in that case both entities shared a table with a type field – Dinomaster Oct 16 '18 at 11:53

1 Answers1

0

Helpful link: Interfaces with hibernate annotations & http://docs.jboss.org/hibernate/core/3.3/reference/en/html/inheritance.html#inheritance-tableperconcrete

Using XML mapping may help as per documentation,

If your superclass is abstract, map it with abstract="true". If it is not abstract, an additional table (it defaults to PAYMENT in the example above), is needed to hold instances of the superclass.