gpt4 book ai didi

java - 需要帮助创建 hbm.xml

转载 作者:行者123 更新时间:2023-12-01 15:23:51 24 4
gpt4 key购买 nike

我是 hibernate 新手,遇到了一个问题。我已经阅读了 hibernate 网站上的入门指南等所有内容,但仍然无法找到解决方案。

我有一堂这样的课:

public class ResultTree {
String attrName;
Map<String, ResultTree> valueMap;
String classValue;
int caseQuant;
Set<Map<String, String>> otherRules;

public String getAttrName() {
return attrName;
}
public void setAttrName(String attrName) {
this.attrName = attrName;
}
public Map<String, ResultTree> getValueMap() {
return valueMap;
}
public void setValueMap(Map<String, ResultTree> valueMap) {
this.valueMap = valueMap;
}
public String getClassValue() {
return classValue;
}
public void setClassValue(String classValue) {
this.classValue = classValue;
}
public int getCaseQuant() {
return caseQuant;
}
public void setCaseQuant(int caseQuant) {
this.caseQuant = caseQuant;
}
public Set<Map<String, String>> getOtherRules() {
return otherRules;
}
public void setOtherRules(Set<Map<String, String>> otherRules) {
this.otherRules = otherRules;
}

}

像这样的类的 hbm.xml 应该是什么样子?我可以自由地创建任何数据结构。

感谢您的帮助,MM

最佳答案

在 Ranna 解决方案的帮助下,我成功地通过将类划分为两个单独的类来对该类进行建模:

public class ResultTree {
private Long id;
private String attrName;
private Map<String, ResultTree> valueMap;
private String classValue;
private int caseQuant;
private Set<Rule> otherRules;
}

public class Rule {
private Long id;
private Map<String, String> terms;
private ResultTree tree;
private String classValue;
}

hbm.xml 具有以下形式:

    <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="lib.experiment.result">
<class name="ResultTree" table="RESULT_TREE">
<id name="id" column="RESULT_TREE_ID" type="long" />
<property name="attrName" type="string" column="ATTR_NAME" />
<property name="classValue" type="string" column="CLASS_VALUE" />
<property name="caseQuant" type="int" column="CASE_QUANT" />
<map name="valueMap" table="RESULT_TREE_LEAF" lazy="false">
<key column="RESULT_TREE_ID"/>
<map-key column="ATTR_VALUE" type="string"/>
<many-to-many class="ResultTree" />
</map>
<set name="otherRules" table="RULE" lazy="false">
<key column="RESULT_TREE_ID"/>
<one-to-many class="Rule"/>
</set>
</class>
</hibernate-mapping>

    <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="lib.experiment.result">
<class name="Rule" table="RULE">
<id name="id" column="RULE_ID" type="long" />
<property name="classValue" column="CLASS" type="string" />
<map name="terms" table="RULE_TERM" lazy="false">
<key column="RULE_ID"/>
<map-key column="ATTR_NAME" type="string"/>
<element column="ATTR_VALUE" type="string"/>
</map>
<many-to-one name="tree" class="ResultTree" lazy="false">
<column name="RESULT_TREE_ID"/>
</many-to-one>
</class>
</hibernate-mapping>

非常感谢您的帮助!

关于java - 需要帮助创建 hbm.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10470258/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com