-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Hibernate Criteria fetches duplicates-one to many associatio
PostPosted: Sat Apr 07, 2012 8:44 am 
Newbie

Joined: Sat Apr 07, 2012 8:32 am
Posts: 1
I have created like component table has one to many relationship with feature table
Even though there are 3 different records in feature table, hibernate Criteria fetches only the last record and displays it for 3 times.
Querying from component table is working fine but the problem is with feature table only
can you please go through my code and give solution

component.hbm.xml

Code:
<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>


feature.hbm.xml

Code:
<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 File

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.