扩展hibernate实体以添加非拥有关系,以同一个表为目标(Extending hibernate entity to add non

编程入门 行业动态 更新时间:2024-10-19 17:20:52
扩展hibernate实体以添加非拥有关系,以同一个表为目标(Extending hibernate entity to add non-owning relationships, targeting the same table)

我使用许多Maven模块开发一个大型模块化项目,并且依赖管理非常复杂。

在我们的核心模块中,我们有一个具有许多属性的实体BackOfficeUser。 该实体用于大多数应用模块。


但是我的模块XXX中的一个需要使用一组实体UserRule来增强用户。

我想知道的是,如果可以将核心BackOfficeUser继承到我的应用程序中创建一个XXXBackOfficeUser,并拥有一个非实体拥有的新关系,并且不需要BackOfficeUser核心数据库表中的新列。

我知道我可以创建一个DAO并调用rulesDAO.findByUser(BackOfficeUser user)但是我想知道是否有可能拥有XXXBackOfficeUser.getRules()

所有这些都不需要修改核心BackOfficeUser类,它被很多其他模块所使用,并且不是MappedSuperClass或其他任何东西,而是常规的hibernate实体。

I work on a large modularized project with many maven modules, and dependency management is quite complex.

In our core module, we have an entity BackOfficeUser with many attributes. 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.

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.

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()

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类,这是不可能的,但是可以在不修改保留BackOfficeUser的表的情况下进行。 BackOffice类所需的修改是:

@Inheritance(strategy = InheritanceType.JOINED)

使用Hibernate,这不会导致将额外的DTYPE列添加到根表中,因为Hibernate支持在没有鉴别器列的情况下连接了继承。 没有鉴别器的加入继承是不可移植的,因为规范不需要它的支持。 结果是,为了定义BackOfficeUser的实际类型,查询将始终加入到新表中。

实施过程大致如下:

//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; }

It is not possible without modification of BackOfficeUser class, but is possible without modifying table where BackOfficeUser is persisted. Modification needed to the BackOffice class is:

@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. 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.

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; }

更多推荐

本文发布于:2023-04-28 00:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329705.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   目标   关系   hibernate   entity

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!