简体   繁体   English

休眠映射最佳实践

[英]Hibernate Mapping best practice

I am wondering if there are any special solutions or software design patterns for the following scenario. 我想知道在以下情况下是否有任何特殊的解决方案或软件设计模式。 I have similar attributes for my Hibernate-Mapping-Classes, like isEnabled, isDetelable, dateCreated, lastModified, ... . 我的Hibernate-Mapping-Classes具有类似的属性,例如isEnabled,isDetelable,dateCreated,lastModified等。 I don't want to repeat the property definition in each class, instead define them once, and add the property to the classes I want. 我不想在每个类中重复属性定义,而是一次定义它们,然后将该属性添加到所需的类中。 For example ClassA can have isEnabled and dateCreated, ClassB can have dateCreated and isDeletable, ClassC can have isEnabled, isDetelable, lastModified and so on. 例如,ClassA可以具有isEnabled和dateCreated,ClassB可以具有dateCreated和isDeletable,ClassC可以具有isEnabled,isDetelable,lastModified等。

One option is to use the @MappedSuperclass annotation to create a superclass for your entities which contains the fields you want. 一种选择是使用@MappedSuperclass注释为包含所需字段的实体创建超类。 Another solution is to use an @Embeddable class that contains the fields and use the @Embedded annotation to add them to only specific entities, in which case you don't need to extend a super entity. 另一个解决方案是使用包含字段的@Embeddable类,并使用@Embedded批注将其仅添加到特定实体,在这种情况下,您无需扩展超级实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM