Search code examples
javaspringhibernatespring-data-jpahibernate-mapping

Override inherited properties (such as inherited relationships table name, columns names...etc) of a @MappedSuperclass in the subclass entity


This is my abstract father:

@MappedSuperclass
public class AbstractEntity{

  @ManyToMany
  protected Set<UserGroupAccess> userGroupAccesses = new HashSet<>();
  
  @ManyToMany
  protected Set<UserAccess> userAccesses = new HashSet<>();

}

these are two entities that inherit the AbstractEntity above:

@Entity
public class Project extends AbstractEntity{
    // some other properties
}

@Entity
public class TodoTask extends AbstractEntity{
    // some other properties
}

I want in each subclass entity to override and have its table of the relationship represented by userGroupAccesses and userAccesses, I mean for example for Project Entity I need it to have its own table projectuserGroupAccesses and a table projectuserAccesses representing the relationship defined in the AbstractEntity superclass.

Previously I was able to solve such issue using the XML representation of entities, I just define the following in each entity XML:

<set name="userGroupAccesses" table="projectusergroupaccesses" cascade="all-delete-orphan">
      <cache usage="read-write" />
      <key column="projectid" />
      <many-to-many class="org.hisp.dhis.user.UserGroupAccess" column="usergroupaccessid" unique="true" />
    </set>

    <set name="userAccesses" table="projectuseraccesses" cascade="all-delete-orphan">
      <cache usage="read-write" />
      <key column="projectid" />
      <many-to-many class="org.hisp.dhis.user.UserAccess" column="useraccessid" unique="true" />
    </set> 

But I still not sure how to do it in the Annotation representation of Entity. I have read about the @AssociationOverrides and @JoinTable I know they are the one I need to solve my issue, but I still couldn't get my head around them.

Can anyone help to explain how to properly use @AssociationOverrides and @JoinTable using my case as an example?


Solution

  • Finally, I managed to know how it's done. The way we use @AssociationOverrides and @JoinTable in my use case to override the parent-defined relation is as the following:

    // Parent class
    @MappedSuperclass
    public class AbstractEntity{
    
      @Id
      @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
      @SequenceGenerator(name = "sequenceGenerator")
      protected Long id; // I need to override the column name in child entities.
    
      @ManyToMany
      protected Set<UserGroupAccess> userGroupAccesses = new HashSet<>(); // I need to override Table name in child entities.
      
      @ManyToMany
      protected Set<UserAccess> userAccesses = new HashSet<>(); // I need to override Table name in child entities.
    
    }
    

    and the child entities I need to change them as the following:

    // First Child Entity
    @Entity
    @AttributeOverride(name = "id", column = @Column(name = "projectid"))
    @AssociationOverride(
        name="userGroupAccesses",
        joinTable=@JoinTable(
            name="projectusergroupaccesses",
            joinColumns=@JoinColumn(name="projectid"),
            inverseJoinColumns=@JoinColumn(name="usergroupaccessid")
        )
    )
    @AssociationOverride(
        name="userAccesses",
        joinTable=@JoinTable(
            name="projectuseraccesses",
            joinColumns=@JoinColumn(name="projectid"),
            inverseJoinColumns=@JoinColumn(name="useraccessid")
        )
    )
    public class Project extends AbstractEntity {
        // some other properties
    }
    
    /////////////////////////
    // Second Child Entity
    
    @Entity
    @AttributeOverride(name = "id", column = @Column(name = "todotaskid"))
    @AssociationOverride(
        name="userGroupAccesses",
        joinTable=@JoinTable(
            name="todotaskusergroupaccesses",
            joinColumns=@JoinColumn(name="todotaskid"),
            inverseJoinColumns=@JoinColumn(name="usergroupaccessid")
        )
    )
    @AssociationOverride(
        name="userAccesses",
        joinTable=@JoinTable(
            name="todotaskuseraccesses",
            joinColumns=@JoinColumn(name="todotaskid"),
            inverseJoinColumns=@JoinColumn(name="useraccessid")
        )
    )
    public class TodoTask extends AbstractEntity {
        // some other properties
    }