[英]Extending hibernate entity to add non-owning relationships, targeting the same table
I work on a large modularized project with many maven modules, and dependency management is quite complex. 我在一个包含许多maven模块的大型模块化项目上工作,并且依赖管理非常复杂。
In our core module, we have an entity BackOfficeUser with many attributes. 在我们的核心模块中,我们有一个具有许多属性的实体BackOfficeUser。 This entity is used in most of the applicative modules. 该实体用于大多数应用模块中。
But one of my module XXX need to enhance the user with a collection of entities UserRule. 但我的一个模块XXX需要使用UserRule实体集合来增强用户。
What i'd like to know is if it's possible to subclass the core BackOfficeUser to create an XXXBackOfficeUser in my application, with a new relationship non owned by the entity, which doesn't need a new column in the core db table of BackOfficeUser. 我想知道的是,是否有可能将核心BackOfficeUser子类化为在我的应用程序中创建一个XXXBackOfficeUser,并且该实体不拥有新关系,后者不需要BackOfficeUser的核心db表中的新列。
I know i can create a DAO and call rulesDAO.findByUser(BackOfficeUser user)
But i'd like to know if it's possible to have instead XXXBackOfficeUser.getRules()
我知道我可以创建一个DAO并调用rulesDAO.findByUser(BackOfficeUser user)
但是我想知道是否可以使用XXXBackOfficeUser.getRules()
All that without modifying the core BackOfficeUser class which is used by a LOT of other modules, and which is not a MappedSuperClass or anything else but a regular hibernate entity. 所有这些都没有修改由许多其他模块使用的核心BackOfficeUser类,而不是MappedSuperClass或除常规hibernate实体之外的任何其他模块。
It is not possible without modification of BackOfficeUser class, but is possible without modifying table where BackOfficeUser is persisted. 如果不修改BackOfficeUser类是不可能的,但是可以在不修改BackOfficeUser持久化的表的情况下实现。 Modification needed to the BackOffice class is: BackOffice类所需的修改是:
@Inheritance(strategy = InheritanceType.JOINED)
With Hibernate this does not cause additional DTYPE column to be added to the root-table, because Hibernate support joined inheritance without discriminator column. 使用Hibernate时,这不会导致将额外的DTYPE列添加到根表中,因为Hibernate支持在没有discriminator列的情况下加入了继承。 Joined inheritance without discriminator is not portable, because specification does not require support for it. 没有鉴别器的连接继承是不可移植的,因为规范不需要支持它。 Consequence is that for defining actual type of BackOfficeUser, queries will always be joined to the new table. 结果是,为了定义BackOfficeUser的实际类型,查询将始终连接到新表。
Implementation goes roughly as follows: 实施大致如下:
//Existing BackOfficeUser, @Inheritance is added
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class BackOfficeUser {
@Id private Integer id;
}
//New class that extends BackOfficeUser. Will have dedicated
//table for added persistent attributes, shared attributes
//are persisted to existing table "BackOfficeUser"
@Entity
public class XxxBackOfficeUser extends BackOfficeUser {
private String someAttribute
@ManyToMany (mappedBy = "xxxBackOfficeUsers")
private List<Rule> rules;
}
//New entity class which does have relation to XxxBackOfficeUser
@Entity
public class Rule {
@Id private Integer id;
@ManyToMany
private List<XxxBackOfficeUser> xxxBackOfficeUsers;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.