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

huangapple go评论68阅读模式
英文:

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

问题

这是我的抽象父类:

@MappedSuperclass
public class AbstractEntity {

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

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

}

以下是继承上述 AbstractEntity 的两个实体:

@Entity
public class Project extends AbstractEntity {
    // 其他属性
}

@Entity
public class TodoTask extends AbstractEntity {
    // 其他属性
}

我希望在每个子类实体中覆盖并拥有由 userGroupAccessesuserAccesses 表示的关系的表。举例来说,对于 Project 实体,我希望它有自己的表 projectuserGroupAccesses 和一个表 projectuserAccesses,来表示在 AbstractEntity 超类中定义的关系。

以前,我可以通过实体的 XML 表示来解决这种问题,我只需要在每个实体的 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>

但我仍然不确定如何在实体的注解表示中实现它。我已经阅读了关于 @AssociationOverrides@JoinTable 的内容,我知道它们是我需要解决问题的注解,但我仍然无法完全理解。

有人能帮助解释如何正确使用 @AssociationOverrides@JoinTable 并以我的情况为例吗?

英文:

This is my abstract father:

@MappedSuperclass
public class AbstractEntity{

  @ManyToMany
  protected Set&lt;UserGroupAccess&gt; userGroupAccesses = new HashSet&lt;&gt;();
  
  @ManyToMany
  protected Set&lt;UserAccess&gt; userAccesses = new HashSet&lt;&gt;();

}

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:

&lt;set name=&quot;userGroupAccesses&quot; table=&quot;projectusergroupaccesses&quot; cascade=&quot;all-delete-orphan&quot;&gt;
      &lt;cache usage=&quot;read-write&quot; /&gt;
      &lt;key column=&quot;projectid&quot; /&gt;
      &lt;many-to-many class=&quot;org.hisp.dhis.user.UserGroupAccess&quot; column=&quot;usergroupaccessid&quot; unique=&quot;true&quot; /&gt;
    &lt;/set&gt;

    &lt;set name=&quot;userAccesses&quot; table=&quot;projectuseraccesses&quot; cascade=&quot;all-delete-orphan&quot;&gt;
      &lt;cache usage=&quot;read-write&quot; /&gt;
      &lt;key column=&quot;projectid&quot; /&gt;
      &lt;many-to-many class=&quot;org.hisp.dhis.user.UserAccess&quot; column=&quot;useraccessid&quot; unique=&quot;true&quot; /&gt;
    &lt;/set&gt; 

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?

答案1

得分: 4

最终,我成功地知道了如何完成它。
在我的用例中,我们使用 @AssociationOverrides@JoinTable 来覆盖父类定义的关联关系,具体步骤如下所示:

// 父类
@MappedSuperclass
public class AbstractEntity{

  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
  @SequenceGenerator(name = "sequenceGenerator")
  protected Long id; // 我需要在子类实体中覆盖列名。

  @ManyToMany
  protected Set<UserGroupAccess> userGroupAccesses = new HashSet<>(); // 我需要在子类实体中覆盖表名。
  
  @ManyToMany
  protected Set<UserAccess> userAccesses = new HashSet<>(); // 我需要在子类实体中覆盖表名。

}

以下是我需要更改的子类实体示例:

// 第一个子类实体
@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 {
    // 其他属性
}

/////////////////////////
// 第二个子类实体

@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 {
    // 其他属性
}
英文:

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 = &quot;sequenceGenerator&quot;)
  @SequenceGenerator(name = &quot;sequenceGenerator&quot;)
  protected Long id; // I need to override the column name in child entities.

  @ManyToMany
  protected Set&lt;UserGroupAccess&gt; userGroupAccesses = new HashSet&lt;&gt;(); // I need to override Table name in child entities.
  
  @ManyToMany
  protected Set&lt;UserAccess&gt; userAccesses = new HashSet&lt;&gt;(); // 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 = &quot;id&quot;, column = @Column(name = &quot;projectid&quot;))
@AssociationOverride(
    name=&quot;userGroupAccesses&quot;,
    joinTable=@JoinTable(
        name=&quot;projectusergroupaccesses&quot;,
        joinColumns=@JoinColumn(name=&quot;projectid&quot;),
        inverseJoinColumns=@JoinColumn(name=&quot;usergroupaccessid&quot;)
    )
)
@AssociationOverride(
    name=&quot;userAccesses&quot;,
    joinTable=@JoinTable(
        name=&quot;projectuseraccesses&quot;,
        joinColumns=@JoinColumn(name=&quot;projectid&quot;),
        inverseJoinColumns=@JoinColumn(name=&quot;useraccessid&quot;)
    )
)
public class Project extends AbstractEntity {
    // some other properties
}

/////////////////////////
// Second Child Entity

@Entity
@AttributeOverride(name = &quot;id&quot;, column = @Column(name = &quot;todotaskid&quot;))
@AssociationOverride(
    name=&quot;userGroupAccesses&quot;,
    joinTable=@JoinTable(
        name=&quot;todotaskusergroupaccesses&quot;,
        joinColumns=@JoinColumn(name=&quot;todotaskid&quot;),
        inverseJoinColumns=@JoinColumn(name=&quot;usergroupaccessid&quot;)
    )
)
@AssociationOverride(
    name=&quot;userAccesses&quot;,
    joinTable=@JoinTable(
        name=&quot;todotaskuseraccesses&quot;,
        joinColumns=@JoinColumn(name=&quot;todotaskid&quot;),
        inverseJoinColumns=@JoinColumn(name=&quot;useraccessid&quot;)
    )
)
public class TodoTask extends AbstractEntity {
    // some other properties
}

huangapple
  • 本文由 发表于 2020年8月30日 23:51:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63659285.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定