Hibernate Mapped Superclass relationships and overriding

15,193

A mapped superclass is not an Entity, it can't be part of an association. So map your classes as entities and either introduce a mapped superclass "above" them or use a TABLE_PER_CLASS strategy.

See also

Share:
15,193
Andrew
Author by

Andrew

Updated on September 15, 2022

Comments

  • Andrew
    Andrew over 1 year

    I have an abstract MappedSuperClass, Participant, which is extended by three kinds of 'Participant'. Each one then uses its own kind of 'Project', also an abstract MappedSuperClass. However, I want the base class to know about Projects so I can write generic code to interact with Participants. How do I specify this using Hibernate annotations? and how will I override it in the ExtendedParticipant and ExtendedProject classes?

    Each Participant type, and each Project type, have their own database tables with existing data and ids (not unique across tables) that I cannot change.

    The following code gives me the IDE error "Many to one attribute should not be 'Mapped Superclass'".

    @MappedSuperclass
    public abstract class Participant implements Persistable {
    
        ...
    
        @ManyToOne
        @JoinColumn(name = "project_id")
        public Project getProject() {
            return project;
        }
    
        public void setProject(Project project) {
            this.project = project;
        }
    
        ...
    }
    

    and the Project class is much the same with the same problem:

    @MappedSuperclass
    public abstract class Project implements Persistable {
    
        ...
    
        @OneToMany
        public List<Participant> getParticipants() {
            return participants;
        }
    
        public void setProject(List<Participant> participants) {
            this.participants = participants;
        }
    
        ...
    }
    
  • Andrew
    Andrew almost 14 years
    Thanks Pascal, that make sense. I saw another post you had commented on about this but I'm still unsure what to do about it. There must be a way to set up all the relationships using base classes?
  • Devanshu Mevada
    Devanshu Mevada almost 14 years
    @Andrew: Well, why are they mapped superclass? Can't you put the common properties in a class above them? Another "option" would be to map them as entities and use a TABLE_PER_CLASS strategy.
  • Andrew
    Andrew almost 14 years
    The 3 ExtendedParticipant classes share many field names, so I had hoped to put all that into the base class, which is why I chose a Mapped SuperClass. I guess I could have a base class with no mappings, but then I would have to include every field in every ExtendedParticipant...