Java 需要帮助创建hbm.xml吗

Java 需要帮助创建hbm.xml吗,java,hibernate,orm,hbm,Java,Hibernate,Orm,Hbm,我刚开始冬眠,遇到了一个难题。我已经通过hibernate网站上的入门指南等阅读了所有内容,但仍然无法找到解决方案 我有一门课是这样的: public class ResultTree { String attrName; Map<String, ResultTree> valueMap; String classValue; int caseQuant; Set<Map<String, String>> otherRules; public String

我刚开始冬眠,遇到了一个难题。我已经通过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;
}
公共类结果树{
字符串属性名;
地图价值地图;
字符串类值;
int caseQuant;
制定其他规则;
公共字符串getAttrName(){
返回属性名;
}
public void setAttrName(字符串attrName){
this.attrName=attrName;
}
公共地图getValueMap(){
返回值映射;
}
public void setValueMap(Map valueMap){
this.valueMap=valueMap;
}
公共字符串getClassValue(){
返回类值;
}
公共void setClassValue(字符串classValue){
this.classValue=classValue;
}
public int getCaseQuant(){
返回案例数量;
}
public void setCaseQuant(int caseQuant){
this.caseQuant=caseQuant;
}
公共集getOtherRules(){
返回其他规则;
}
public void setOtherRules(Set otherRules){
this.otherRules=otherRules;
}
}

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

谢谢你的帮助,
希望这能对你有所帮助

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.ResultTree" table="result_treeid">
<meta attribute="class-description">This class contains student details.</meta>
<id name="id" type="long" column="id">
<generator class="native" />
</id>
<property name="attrName" type="string" length="100" not-null="true" column="attr_name" />
<property name="classValue" type="string" length="100" not-null="true" column="class_value" />
<property name="caseQuant" type="bigint" not-null="true" column="case_quant" />
<map role="valueMap" table="value_map">
     <key column="id"/>
     <map-key column="keyname" type="string"/>
     <element column="valuename" type="ResultTree"/>
</map>
<map role="otherRules" table="other_rules">
     <key column="id"/>
     <map-key column="keyname" type="string"/>
     <element column="valuename" type="string"/>
</map>
</class>
</hibernate-mapping>

该类包含学生详细信息。

在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;
}
公共类结果树{
私人长id;
私有字符串名称;
私人地图价值地图;
私有字符串类值;
私人内部案件数量;
私人制定其他规则;
}

公共类规则{
私人长id;
专用地图术语;
私有结果树;
私有字符串类值;
}
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>



非常感谢你的帮助

你为什么需要套装。。你可以通过简单的地图来实现这一点。。是否有任何特定的需求?您必须简化您的模型,使其与关系模型保持一致。正如Ranna已经指出的,在一个集合中使用该映射实际上不是映射到关系数据库的东西。即使你找到了如何映射它,使用它也将是一个巨大的痛苦。@Ranna该类正在用多条路径为决策树建模。如果发现一个无法识别的值,则该集基本上是一组包含对的集。基本上,这是一个需求清单。你是对的。但是一个值可以有多个值,并且可以有多个键。看,我坚持认为,就我所知,你不能地图集。。使用映射,您将实现函数性..映射本身提供值列表。。所以你不需要一套地图。。用映射实体跟随答案。最好都是D。。
    <?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>