2012-04-04 91 views
1

部件 - >休眠標準提取重複 - 一對多關聯

 --> feature 1 

     --> feature 2 

     --> feature 3 

組件具有一個與特徵一對多關係

即使有在特徵表3個不同的記錄,休眠標準僅取最後一條記錄並顯示3次。

我給我的hbm文件和標準代碼。

從成分表查詢是好的,但問題是功能表僅

component.hbm.xml

<class name="com.arv.RelationMapping.component" table="component" > 

    <id name="componentPK" column="component_pk" type="java.lang.Long"/> 
    <property name="componentName" column="component_name" type="java.lang.String"/> 

    <set name="feature" table="feature" inverse="true">  
     <key> 
      <column name="component_pk"/> 
     </key>  
     <one-to-many class="com.arv.RelationMapping.feature" />  
    </set> 

</class> 

features.hbm.xml

<class name="com.arv.RelationMapping.feature" table="feature"> 

    <id name="featurePK" column="feature_pk" type="java.lang.Long"/> 

    <many-to-one name="component" class="com.arv.RelationMapping.component" fetch="select"> 
     <column name="component_pk"/> 
    </many-to-one> 

    <property name='scenarioId' column="scenario_id" type="java.lang.String"/> 
    <property name='scenarioDesc' column="scenario_desc" type="java.lang.String"/> 
    <property name='testCaseFile' column="test_case_file" type="java.lang.String"/> 

</class> 

Java類

公共類測試的{

public static void main(String[] args) 
{ 
    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); 
    Session session = sessionFactory.openSession(); 

    Criteria criteria = session.createCriteria(feature.class); 
    List summaryList = criteria.list(); 
    feature feature = new feature(); 

    System.out.println(summaryList.size()); // getting size correctly 

    if(summaryList !=null) 
    { 
     for(Object obj:summaryList) 
     { 
      feature = (feature)obj; 

        // getting same values for each loop 
      System.out.println(feature.getScenarioDesc()); 
      System.out.println(feature.getScenarioId()); 
      System.out.println(feature.getFeaturePK()); 

     } 
    } 
    session.close();  
} 

}

+0

'SCENARIO_PK'從哪裏來? – 2012-04-04 19:34:32

回答

2

使用criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);威力^ h ELP。

+0

但是爲什麼?你能解釋一下嗎? – jhegedus 2014-02-10 10:14:54

0
public static void main(String[] args) 
{ 
    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); 
    Session session = sessionFactory.openSession(); 

    Criteria criteria = session.createCriteria(feature.class); 
    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY); 
    List summaryList = criteria.list(); 
    feature feature = new feature(); 

    System.out.println(summaryList.size()); // getting size correctly 

    if(summaryList !=null) 
    { 
     for(Object obj:summaryList) 
     { 
      feature = (feature)obj; 

        // getting same values for each loop 
      System.out.println(feature.getScenarioDesc()); 
      System.out.println(feature.getScenarioId()); 
      System.out.println(feature.getFeaturePK()); 

     } 
    } 
    session.close();  
}