简体   繁体   English

扩展hibernate实体以添加非拥有关系,以同一个表为目标

[英]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.

相关问题 从Hibernate中的非拥有实体查询拥有实体 - Querying the owning entity from the non-owning entity in Hibernate 在Hibernate @ManyToMany双向映射中保留非所有者副对象不会在联接表中保留数据 - Persisting non-owning side object in Hibernate @ManyToMany bidirectional mapping does not persist data in joining table @OneToOne/@ManyToOne/@ManyToMany 的非拥有实体端 - Non-owning entity side of @OneToOne/@ManyToOne/@ManyToMany 拥有侧与非拥有侧在休眠中及其在按元素映射的引用中的用法? - Owning Side vs Non-Owning Side In hibernate and its usage in reference of mapped by element? JPA QL用于选择ManytoMany关系的非所有权方吗? - JPA QL for selecting non-owning side of ManytoMany relationship? 休眠:实体关系和额外表 - Hibernate: entity relationships and extra table 使用非主端上的主/外键映射来映射JPA双向@OneToOne - Mapping a JPA bi-directional @OneToOne with primary/foreign key mapping on non-owning side Hibernate,如果拥有的实体相同,则使用已保存在数据库中的实体与拥有方链接,而不是创建新实体 - Hibernate, use the entity already persisted in DB to link with owning side instead of creating a new one if the owned entity is same 拥有共享主键的Hibernate拥有实体端 - Hibernate owning entity side with shared primary key Hibernate ManyToMany映射更新非拥有方 - Hibernate ManyToMany mapping updating the non owning side
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM