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

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

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

问题

这是我的抽象父类:

  1. @MappedSuperclass
  2. public class AbstractEntity {
  3. @ManyToMany
  4. protected Set<UserGroupAccess> userGroupAccesses = new HashSet<>();
  5. @ManyToMany
  6. protected Set<UserAccess> userAccesses = new HashSet<>();
  7. }

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

  1. @Entity
  2. public class Project extends AbstractEntity {
  3. // 其他属性
  4. }
  5. @Entity
  6. public class TodoTask extends AbstractEntity {
  7. // 其他属性
  8. }

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

以前,我可以通过实体的 XML 表示来解决这种问题,我只需要在每个实体的 XML 中定义以下内容:

  1. <set name="userGroupAccesses" table="projectusergroupaccesses" cascade="all-delete-orphan">
  2. <cache usage="read-write" />
  3. <key column="projectid" />
  4. <many-to-many class="org.hisp.dhis.user.UserGroupAccess" column="usergroupaccessid" unique="true" />
  5. </set>
  6. <set name="userAccesses" table="projectuseraccesses" cascade="all-delete-orphan">
  7. <cache usage="read-write" />
  8. <key column="projectid" />
  9. <many-to-many class="org.hisp.dhis.user.UserAccess" column="useraccessid" unique="true" />
  10. </set>

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

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

英文:

This is my abstract father:

  1. @MappedSuperclass
  2. public class AbstractEntity{
  3. @ManyToMany
  4. protected Set&lt;UserGroupAccess&gt; userGroupAccesses = new HashSet&lt;&gt;();
  5. @ManyToMany
  6. protected Set&lt;UserAccess&gt; userAccesses = new HashSet&lt;&gt;();
  7. }

these are two entities that inherit the AbstractEntity above:

  1. @Entity
  2. public class Project extends AbstractEntity{
  3. // some other properties
  4. }
  5. @Entity
  6. public class TodoTask extends AbstractEntity{
  7. // some other properties
  8. }

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:

  1. &lt;set name=&quot;userGroupAccesses&quot; table=&quot;projectusergroupaccesses&quot; cascade=&quot;all-delete-orphan&quot;&gt;
  2. &lt;cache usage=&quot;read-write&quot; /&gt;
  3. &lt;key column=&quot;projectid&quot; /&gt;
  4. &lt;many-to-many class=&quot;org.hisp.dhis.user.UserGroupAccess&quot; column=&quot;usergroupaccessid&quot; unique=&quot;true&quot; /&gt;
  5. &lt;/set&gt;
  6. &lt;set name=&quot;userAccesses&quot; table=&quot;projectuseraccesses&quot; cascade=&quot;all-delete-orphan&quot;&gt;
  7. &lt;cache usage=&quot;read-write&quot; /&gt;
  8. &lt;key column=&quot;projectid&quot; /&gt;
  9. &lt;many-to-many class=&quot;org.hisp.dhis.user.UserAccess&quot; column=&quot;useraccessid&quot; unique=&quot;true&quot; /&gt;
  10. &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 来覆盖父类定义的关联关系,具体步骤如下所示:

  1. // 父类
  2. @MappedSuperclass
  3. public class AbstractEntity{
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
  6. @SequenceGenerator(name = "sequenceGenerator")
  7. protected Long id; // 我需要在子类实体中覆盖列名。
  8. @ManyToMany
  9. protected Set<UserGroupAccess> userGroupAccesses = new HashSet<>(); // 我需要在子类实体中覆盖表名。
  10. @ManyToMany
  11. protected Set<UserAccess> userAccesses = new HashSet<>(); // 我需要在子类实体中覆盖表名。
  12. }

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

  1. // 第一个子类实体
  2. @Entity
  3. @AttributeOverride(name = "id", column = @Column(name = "projectid"))
  4. @AssociationOverride(
  5. name="userGroupAccesses",
  6. joinTable=@JoinTable(
  7. name="projectusergroupaccesses",
  8. joinColumns=@JoinColumn(name="projectid"),
  9. inverseJoinColumns=@JoinColumn(name="usergroupaccessid")
  10. )
  11. )
  12. @AssociationOverride(
  13. name="userAccesses",
  14. joinTable=@JoinTable(
  15. name="projectuseraccesses",
  16. joinColumns=@JoinColumn(name="projectid"),
  17. inverseJoinColumns=@JoinColumn(name="useraccessid")
  18. )
  19. )
  20. public class Project extends AbstractEntity {
  21. // 其他属性
  22. }
  23. /////////////////////////
  24. // 第二个子类实体
  25. @Entity
  26. @AttributeOverride(name = "id", column = @Column(name = "todotaskid"))
  27. @AssociationOverride(
  28. name="userGroupAccesses",
  29. joinTable=@JoinTable(
  30. name="todotaskusergroupaccesses",
  31. joinColumns=@JoinColumn(name="todotaskid"),
  32. inverseJoinColumns=@JoinColumn(name="usergroupaccessid")
  33. )
  34. )
  35. @AssociationOverride(
  36. name="userAccesses",
  37. joinTable=@JoinTable(
  38. name="todotaskuseraccesses",
  39. joinColumns=@JoinColumn(name="todotaskid"),
  40. inverseJoinColumns=@JoinColumn(name="useraccessid")
  41. )
  42. )
  43. public class TodoTask extends AbstractEntity {
  44. // 其他属性
  45. }
英文:

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:

  1. // Parent class
  2. @MappedSuperclass
  3. public class AbstractEntity{
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = &quot;sequenceGenerator&quot;)
  6. @SequenceGenerator(name = &quot;sequenceGenerator&quot;)
  7. protected Long id; // I need to override the column name in child entities.
  8. @ManyToMany
  9. protected Set&lt;UserGroupAccess&gt; userGroupAccesses = new HashSet&lt;&gt;(); // I need to override Table name in child entities.
  10. @ManyToMany
  11. protected Set&lt;UserAccess&gt; userAccesses = new HashSet&lt;&gt;(); // I need to override Table name in child entities.
  12. }

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

  1. // First Child Entity
  2. @Entity
  3. @AttributeOverride(name = &quot;id&quot;, column = @Column(name = &quot;projectid&quot;))
  4. @AssociationOverride(
  5. name=&quot;userGroupAccesses&quot;,
  6. joinTable=@JoinTable(
  7. name=&quot;projectusergroupaccesses&quot;,
  8. joinColumns=@JoinColumn(name=&quot;projectid&quot;),
  9. inverseJoinColumns=@JoinColumn(name=&quot;usergroupaccessid&quot;)
  10. )
  11. )
  12. @AssociationOverride(
  13. name=&quot;userAccesses&quot;,
  14. joinTable=@JoinTable(
  15. name=&quot;projectuseraccesses&quot;,
  16. joinColumns=@JoinColumn(name=&quot;projectid&quot;),
  17. inverseJoinColumns=@JoinColumn(name=&quot;useraccessid&quot;)
  18. )
  19. )
  20. public class Project extends AbstractEntity {
  21. // some other properties
  22. }
  23. /////////////////////////
  24. // Second Child Entity
  25. @Entity
  26. @AttributeOverride(name = &quot;id&quot;, column = @Column(name = &quot;todotaskid&quot;))
  27. @AssociationOverride(
  28. name=&quot;userGroupAccesses&quot;,
  29. joinTable=@JoinTable(
  30. name=&quot;todotaskusergroupaccesses&quot;,
  31. joinColumns=@JoinColumn(name=&quot;todotaskid&quot;),
  32. inverseJoinColumns=@JoinColumn(name=&quot;usergroupaccessid&quot;)
  33. )
  34. )
  35. @AssociationOverride(
  36. name=&quot;userAccesses&quot;,
  37. joinTable=@JoinTable(
  38. name=&quot;todotaskuseraccesses&quot;,
  39. joinColumns=@JoinColumn(name=&quot;todotaskid&quot;),
  40. inverseJoinColumns=@JoinColumn(name=&quot;useraccessid&quot;)
  41. )
  42. )
  43. public class TodoTask extends AbstractEntity {
  44. // some other properties
  45. }

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:

确定