java - 如何将两个类 JPA 映射到按类型列区分的同一个表,并让它们具有一个集合,该集合是表的行
问题描述
假设我有这个数据库表设计(键值必须是动态的,所以不,我不能将它们变成单独的列或属性):
餐桌屋,包括:
- ID
- 其他无关紧要的属性,例如名称等
表 house_properties 包括:
- ID
- house_id (fk 指向房子)
- 类型(枚举('外部','内部',...其他))
- 键(字符串)
- 值(字符串)
示例数据:
屋:
- id = 1, name = "随便"
- id = 2, name = "我不在乎这只是示例"
房屋属性:
- id = 100,house_id = 1,type = EXTERIOR,key = 'roof',value = 'shingles'
- id = 101,house_id = 1,type = EXTERIOR,key = 'paint color',value = 'white'
- id = 102,house_id = 1,类型 = EXTERIOR,键 = '百叶窗',值 = '橙色'
- id = 103, house_id = 1, type = INTERIOR, key = '走廊墙壁颜色', value = '绿色'
- id = 104, house_id = 1, type = INTERIOR, key = '厨房地板颜色', value = '蓝色'
- id = 105, house_id = 1, type = INTERIOR, key = '浴室有镜子', value = 'false'
- id = 106,house_id = 2,类型 = EXTERIOR,键 = '屋顶',值 = '金属'
- id = 107,house_id = 2,type = EXTERIOR,key = 'walls',value = 'brick'
- id = 108, house_id = 2, type = EXTERIOR, key = 'windows', value = '双层玻璃'
- id = 109, house_id = 2, type = INTERIOR, key = '墙壁颜色', value = '红色'
- id = 110,house_id = 2,type = INTERIOR,key = '厨房地板',value = 'lenolium'
- id = 110, house_id = 2, type = INTERIOR, key = '浴室有镜子', value = 'true'
我需要将结果属性表映射到由表的“类型”属性值确定的多个类。每个类都应该包含一组键值对。
房子很简单,但我正在尝试找出其他两个类:
@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
}
解决方案
推荐阅读
- c++ - 如何将位置存储在数组中并用于以后在 C++ 中?
- jsf - 通过未封装的selectOneMenu显示表单
- python-3.x - TypeError:不能在类似字节的对象正则表达式上使用字符串模式,没有获得标准
- javascript - 在 React 中,是否可以更改 map 方法内部的状态,如果不能,有什么替代方法?
- python - 如果子表中的值发生变化,则更新父表的最后修改时间
- tensorflow-datasets - tensorflow 数据集找到了不同版本的请求数据集
- matlab - 预分配单个单元格的内存
- r - 在ggplot R中创建自定义/更多刻度x轴距离
- java - 通过 split() 解析文件名以比较两个常用字段
- java - 辅助函数可以在 Java 中节省内存吗?