使用Hibernate映射模型属性值(MAV)模式

时间:2018-10-22 13:12:44

标签: java hibernate jpa hibernate-mapping

情况: 预先不知道新对象的属性。用户应该能够在不更改架构的情况下设计新对象。 最重要的是,应在创建的对象之间重用不同对象的属性,以简化可比性。

因此,我需要使用关系数据库来实现灵活的架构。 所谓灵活,是指该模式需要支持不同类型的对象,这些对象由不同的属性描述,这在以前是未知的。

示例:

Plane {
  "name" : "..."
  "max_alt" : "..."
}

Submarine {
  "name" : "..."
  "depth_max" : "..."
}

因此,我们将拥有3个描述这两个实体的属性:name,max_alt和depth_max,其中,飞机和潜艇均引用该属性名称。

我的模式如下:

Vehicle       Value                   Attribute
-------  1:n  -----------------  n:1  ----------
ID (PK)       OBJECT_FK    (PK)       ID (PK) 
              ATTRIBUTE_FK (PK)       NAME
              ATTRIBUTE_VALUE

基本上,我已经根据以下方法进行了工作:https://en.wikibooks.org/wiki/Java_Persistence/ManyToMany#Mapping_a_Join_Table_with_Additional_Columns

但是现在我试图“改进”我的JPA映射。我想拥有属于该车辆的所有属性值,并通过该属性进行映射。

像这样:

@Entity
public class Vehicle{
   //obvious code ommited

   **Map<Attribute, Set<AttributeValue>> attributeValueMap;**
}

我已经尝试了很多(请参阅下面的列表),但是我无法使它正常工作。这有可能吗?任何帮助将不胜感激!

JPA Hibernate - Issue with manytomany mapping with extra column in the mapping table https://vladmihalcea.com/the-best-way-to-use-the-manytomany-annotation-with-jpa-and-hibernate/ Self ManyToMany with additional columns using JPA 2.0

PS:我知道使用MAV有一些局限性,但是我们必须坚持使用这种方法和关系数据库。

0 个答案:

没有答案