首页 > 解决方案 > 如何将两个类 JPA 映射到按类型列区分的同一个表,并让它们具有一个集合,该集合是表的行

问题描述

假设我有这个数据库表设计(键值必须是动态的,所以不,我不能将它们变成单独的列或属性):

餐桌屋,包括:

表 house_properties 包括:

示例数据:

屋:

房屋属性:


我需要将结果属性表映射到由表的“类型”属性值确定的多个类。每个类都应该包含一组键值对。

房子很简单,但我正在尝试找出其他两个类:

@Entity
public class House {

    Integer id;

    @OneToMany(mappedBy = "house")
    Collection<ExteriorProperties> exteriorProperties;

    @OneToMany(mappedBy = "house")
    Collection<InteriorProperties> interiorProperties;

    // ... more fields
    // ... getters setters

}

// SINGLE instance for ALL house_properties rows with the same house_id and type = "EXTERIOR"
// How to JPA-map this??
public class ExteriorProperties {

    Integer id;

    @JoinColumn(name = "house_id", referencedColumnName = "id")
    @ManyToOne
    House house; // ref back to house 

    // the collection of key-value pairs that are in all rows with the same house_id and type = "EXTERIOR"
    // How to JPA map this???
    Map<String, String> properties;

    // ... getters, setters
}

// SINGLE instance for ALL house_properties rows with the same house_id and type = "INTERIOR"
// How to JPA-map this??
public class InteriorProperties {

    Integer id;

    @JoinColumn(name = "house_id", referencedColumnName = "id")
    @ManyToOne
    House house; // ref back to house 

    // the collection of key-value pairs that are in all rows with the same house_id and type = "INTERIOR"
    // How to JPA map this???
    Map<String, String> properties;

    // ... getters, setters
}

标签: javajpa

解决方案


推荐阅读