有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java是处理EAV数据模型的错误选择吗?

我一直在尝试使用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是一个包含所有属性的表,可以是用户的属性,也可以是项的属性。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)

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

我使用的是Spring数据JPA


共 (0) 个答案