JPA处理EAV数据模型的选择不好吗?

时间:2015-03-09 01:28:08

标签: java spring hibernate jpa spring-data-jpa

我一直在尝试使用JPA / Hibernate来CRUD操作EAV数据模型并且一直面临很多性能损失。我正在寻找有效管理EAV数据模型的想法。

**(A)**So here is an example of what I am trying to deal with, I have three tables :
UserTable
AttributesTable
UserAttributeValueTable

**(B)**And apart from these three, I have other relations like :
ItemTable
UserItemsRelationTable

基本上,AttributesTable是一个包含所有属性的表,可能是User或Item。 UserAttributeValueTable是连接UserId,AttributeId和值的表。

现在,假设我想要提取所有用户及其属性。

以下是我如何映射它们:

@Entity
UserTable{     
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="USER_ID")
    private long userId;

    @OneToMany(targetEntity=UserAttributeValueTable.class, mappedBy="userTable", fetch=FetchType.LAZY)
    private List<UserAttributeValueTable> userAttributeValues;

    //Other Columns        
}

@Entity
UserAttributeValueTable{

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="USER_ATTRIBUTE_VALUE_ID")
    private long userAttributeValueId;     

    @Column(name="\"VALUE\"")
    private String value;

    //bi-directional many-to-one association to AttributesTable 
    @ManyToOne(targetEntity=AttributesTable .class ,fetch=FetchType.LAZY)
    @JoinColumn(name="ATTRIBUTE_ID")
    private AttributesTable attributes;

    //bi-directional many-to-one association to UserTable 
    @ManyToOne(targetEntity=UserTable.class,fetch=FetchType.LAZY)
    @JoinColumn(name="USER_ID")
    @JsonIgnore
    private UserTable user;

    //Other columns

}


@Entity
AttributeTable{

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="ATTRIBUTE_ID")
    private long attrId;

    @Column(name="ATTRIBUTE_NAME")
    private String attrName;

    //Other columns
}

问题: 1.当我执行findAll时,我遇到了一个无限循环,这很明显,所以我使用了@JsonIgnore,因此它会忽略UsersTable。但是,要检索300个用户(用户属性值),需要花费大量时间,而且我也不会查找每个列。我只想以这种方式压扁它:

User :[{ id:"1", name:"John", otherAttributes={attr1:"value1",attr2:"value2"},{ id:"2", name:"Joey", otherAttributes={attr1:"value1",attr2:"value2"}]
  1. 与其他类型的关系(如(B)
  2. )进行映射时的映射复杂性

    在编写简单的SQL查询后,我觉得在这些动态模型中更容易,其中列不是字段而是属性。有什么建议吗?

    我正在使用Spring Data JPA。

0 个答案:

没有答案
相关问题