英文:
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 {
// 其他属性
}
我希望在每个子类实体中覆盖并拥有由 userGroupAccesses 和 userAccesses 表示的关系的表。举例来说,对于 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<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?
答案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 = "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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论