1

component -->

      --> feature 1 

      --> feature 2

      --> feature 3

component has one to many relationship with feature

Even though there are 3 different records in feature table, hibernate Criteria fetches only the last record and displays it for 3 times.

I give my hbm file and also criteria code.

Querying from component table is fine but the problem is with feature table only

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 class

public class Test {

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

}

4

2 回答 2

2

使用criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);可能会有所帮助。

于 2012-04-23T12:52:32.610 回答
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();    
}
于 2012-04-24T18:20:12.777 回答