0

I'm new to hibernate and I came a cross a problem. I've read all throu the starter guides etc. on the hibernate website and I still can't come up with a solution.

I have a class like this:

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;
}

}

How should a hbm.xml for a class like this look? I'm free to create any data structure.

Thanks for the help, MM

kemaleq
  • 23
  • 6
  • why do you need Set>.. you can acheive that by simply Map.. is there any specific requirement ? – PVR May 06 '12 at 11:55
  • You'll have to simplify your model to bring it in line with the relational model. As Ranna already pointed out, using that map within a set is really not something you map to the relational DB. Even if you find out how to map it, it's going to be a huge pain to use. – Marko Topolnik May 06 '12 at 12:28
  • @Ranna That class is modeling a decision tree with an adition of multiple paths if an unrecognized value is found, the Set> is basicly a set of sets containing pairs . Basicly it's a list of requirements. – kemaleq May 06 '12 at 13:07
  • You are true. But one value can have many value and many keys are possible. See i m insisting as , as far my knowledge you can not map Set>.. Using Map you will acheive the functionlity.. – PVR May 06 '12 at 16:17
  • Map itself provides list of values.. so you dont need a SET of MAP.. follow the answer with Map entity.All D Best.. – PVR May 06 '12 at 16:18
  • @Ranna Not exactly - there is a possibility that two "rules" (Map objects) have different values of the same attribute so I can't put them all into one Map. Would it work if I made another class containing only the Map object and mapped a set of said classes into my present class? – kemaleq May 06 '12 at 18:45
  • @kemaleq - yeah this would definitely work. – PVR May 07 '12 at 03:54

2 Answers2

1

With the help of Ranna's solution I managed to model the class by dividing it to two separate classes:

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

and

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

hbm.xml has the following form:

    <?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>

and

    <?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>

Thanks a lot for the help!

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
kemaleq
  • 23
  • 6
0

Hope this would help you.

<?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>
PVR
  • 2,534
  • 18
  • 38