使用 Hibernate 为用户实现 EAV 模式 ->设置关系
我正在尝试使用 Java/Spring MVC 和 Hibernate 在我的 Web 应用程序中设置一个简单的 EAV 模式。我似乎无法弄清楚这种情况下 hibernate XML 设置背后的魔力。
我的数据库表“SETUP”有三列:
- user_id (FK)
- setup_item
- setup_value
数据库组合键由 user_id | user_id | user_id (FK) setup_item setup_value 组成。 setup_item
这是Setup.java类:
public class Setup implements CommonFormElements, Serializable {
private Map data = new HashMap();
private String saveAction;
private Integer speciesNamingList;
private User user;
Logger log = LoggerFactory.getLogger(Setup.class);
public String getSaveAction() {
return saveAction;
}
public void setSaveAction(String action) {
this.saveAction = action;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Integer getSpeciesNamingList() {
return speciesNamingList;
}
public void setSpeciesNamingList(Integer speciesNamingList) {
this.speciesNamingList = speciesNamingList;
}
public Map getData() {
return data;
}
public void setData(Map data) {
this.data = data;
}
}
我的Hibernate设置问题是,我似乎无法弄清楚如何映射外键和映射的键将构造复合键的事实表的...这是由于缺乏使用 Hibernate 的经验。这是我最初尝试让它发挥作用:
<composite-id>
<key-many-to-one foreign-key="id" name="user" column="user_id" class="Business.User">
<meta attribute="use-in-equals">true</meta>
</key-many-to-one>
</composite-id>
<map lazy="false" name="data" table="setup">
<key column="user_id" property-ref="user"/>
<composite-map-key class="Command.Setup">
<key-property name="data" column="setup_item" type="string"/>
</composite-map-key>
<element column="setup_value" not-null="true" type="string"/>
</map>
任何有关如何正确映射这种常见场景的见解将不胜感激!
I'm trying to setup a simple EAV pattern in my web app using Java/Spring MVC and Hibernate. I can't seem to figure out the magic behind the hibernate XML setup for this scenario.
My database table "SETUP" has three columns:
- user_id (FK)
- setup_item
- setup_value
The database composite key is made up of user_id | setup_item
Here's the Setup.java class:
public class Setup implements CommonFormElements, Serializable {
private Map data = new HashMap();
private String saveAction;
private Integer speciesNamingList;
private User user;
Logger log = LoggerFactory.getLogger(Setup.class);
public String getSaveAction() {
return saveAction;
}
public void setSaveAction(String action) {
this.saveAction = action;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Integer getSpeciesNamingList() {
return speciesNamingList;
}
public void setSpeciesNamingList(Integer speciesNamingList) {
this.speciesNamingList = speciesNamingList;
}
public Map getData() {
return data;
}
public void setData(Map data) {
this.data = data;
}
}
My problem with the Hibernate setup, is that I can't seem to figure out how to map out the fact that a foreign key and the key of a map will construct the composite key of the table... this is due to a lack of experience using Hibernate. Here's my initial attempt at getting this to work:
<composite-id>
<key-many-to-one foreign-key="id" name="user" column="user_id" class="Business.User">
<meta attribute="use-in-equals">true</meta>
</key-many-to-one>
</composite-id>
<map lazy="false" name="data" table="setup">
<key column="user_id" property-ref="user"/>
<composite-map-key class="Command.Setup">
<key-property name="data" column="setup_item" type="string"/>
</composite-map-key>
<element column="setup_value" not-null="true" type="string"/>
</map>
Any insight into how to properly map this common scenario would be most appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您自己所示,您有一个不一致映射
您说Setup类定义了一个复合主键(注意我已经创建了一个复合主键类(SetupId - 见下文),它必须实现Serialized、equals和hashcode方法)
因为您的Setup类有一个值类型的Map,所以您在定义其关系时应该定义其复合外键(参见key元素)
,同时使用复合外键列作为映射键(USER_ID,对吗?),这没有意义。为什么 ?
除此之外,Hibernate 不支持自动生成复合主键
假设这里有你的 SETUP 表
和你的 DATA_TABLE
什么无论您尝试以下操作,都会发生这种情况。
由于 SETUP 表没有定义值为 3 的 USER_ID,您将看到约束违规。
牢记在心
As shown by yourself, you have a inconsistent mapping
You said Setup class defines a composite primary key (Notice i have created a composite primary key class (SetupId - see bellow) which must implements Serializable and equals and hashcode method)
Because of your Setup class has a Map of value Type, you should define its composite foreign key when defining its relationship (see key element)
And, at the same time, use a composite foreign key column as map-key (USER_ID, right ?) which does not make sense. Why ?
Besides that, Hibernate does not support automatic generation of composite primary key
Suppose here goes your SETUP Table
And your DATA_TABLE
What happens whether you try the following one
Because of SETUP Table does not define a USER_ID which value is 3, you will see a constraint violation.
Keep it in mind