2012-05-06 176 views
0

我是新來的休眠,我遇到了一個問題。我已經閱讀了所有通過啓動指南等在休眠網站上,我仍然不能提出一個解決方案。需要幫助創建hbm.xml

我有這樣一個類:

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

+0

你爲什麼需要Set > ..你可以通過簡單的Map 實現這個功能。是否有任何特定的要求? – PVR

+0

您必須簡化模型才能使其與關係模型保持一致。正如Ranna已經指出的那樣,在集合中使用該映射實際上不是您映射到關係數據庫的東西。即使你知道如何映射它,使用它也將是一個巨大的痛苦。 –

+0

@Ranna如果找到一個無法識別的值,那麼這個類正在建立一個具有多條路徑的決策樹,Set >基本上是一組包含對的集合。 基本上它是一個需求清單。 – kemaleq

回答

1

隨着空間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> 

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> 

非常感謝您的幫助!

0

希望這會幫助你。

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