-->
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.  [ 7 posts ] 
Author Message
 Post subject: Hibernate returns Duplicate Data
PostPosted: Thu Aug 12, 2010 4:43 pm 
Newbie

Joined: Thu Aug 12, 2010 4:11 pm
Posts: 2
Hi,

I am new to Hibernate. I have written a simple Hibernate query to return the data from a Oracle (View). Based on the where condition there are 15 records in Database. When i run my Hibernate query it returns 15 rows but it is returning first record 15 times. Please help me in resolving this problem.

Here is my Pojo Class SPView.Java

public class SPView implements java.io.Serializable {

private Long sPid;
private Long sid;
private Long acctid;
private Long swid;
private String fName;

}

It has respective setters and getters

My hbm file is SPView.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!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.SPView" table="S_P_VIEW" schema="xyz">
<id name="sPid" type="java.lang.Long">
<column name="S_P_ID" precision="18" scale="0" />
<generator class="assigned" />
</id>

<property name="sid" type="java.lang.Long">
<column name="S_ID" precision="20" scale="0" not-null="true" />
</property>
<property name="acctid" type="java.lang.Long">
<column name="ACCT_ID" precision="18" scale="0" not-null="true" />
</property>
<property name="swid" type="java.lang.Long">
<column name="SW_ID" precision="18" scale="0" />
</property>
<property name="fName" type="java.lang.String">
<column name="F_NAME" length="64" not-null="true" />
</property>

<query name="getSPVWDetails">
<![CDATA[FROM SPView a WHERE a.sPSid= :spSid]]>
</query>

</class>
</hibernate-mapping>

public ArrayList getSPVwDetails(Long spSid)
{
//Session session = null;
SPView spv = null;
List<SPView> spViewList = new ArrayList<SPView>();
session = getSession();
Query query = session.getNamedQuery("com.SPView.getSPVWDetails");
query.setLong("spSid", spSid);
System.out.println("Query" + query.toString());
spViewList = query.list();
System.out.println("List Size " + query.list().size());
for(int index=0; index<spViewList.size();index++)
{
spv= spViewList.get(index);
System.out.println(" Service Feature Name = " + spv.getFName());
System.out.println(" Service Feature Sid = " + spv.getSFSid());

System.out.println("Index" + index);
System.out.println("--------------------------");

}

session.close();
return (ArrayList) spViewList;

}


Is it the tables in that view is creating the problem ?

Thanks,
Sam


Top
 Profile  
 
 Post subject: Re: Hibernate returns Duplicate Data
PostPosted: Fri Aug 13, 2010 1:42 am 
Regular
Regular

Joined: Fri Aug 06, 2010 1:49 am
Posts: 102
Location: shynate26@gmail.com
Hi gsamanthula,

There is a contradiction in calling the named query.In the service coding namedquery says com.SPView.getSPVWDetails where as in the mapping it says <query name="getSPVWDetails"> . Please correct and try again. Also add show_sql=true in configuration file of hibernate. So that you will know which query is fired.

_________________

Cheers!
Shynate
mailto:shynate26@gmail.com
www.CSSCORP.com


Top
 Profile  
 
 Post subject: Re: Hibernate returns Duplicate Data
PostPosted: Fri Aug 13, 2010 10:12 am 
Newbie

Joined: Thu Aug 12, 2010 4:11 pm
Posts: 2
Hi Shynate,

Thanks for your help. I donot have problem calling the named query. The query is being printed on the console. When i execute the query in database it fetches 15 different records but when i run through Java , first record is looping fifteen times.When i debug the code i see fifteen objects in querylist and all have the same data.

Thanks,
Sam


Top
 Profile  
 
 Post subject: Re: Hibernate returns Duplicate Data
PostPosted: Thu Oct 07, 2010 2:35 pm 
Newbie

Joined: Wed Oct 06, 2010 12:49 pm
Posts: 8
I have the same issue. I haven't been able to get any answers in two days of scouring the internet.


Top
 Profile  
 
 Post subject: Re: Hibernate returns Duplicate Data
PostPosted: Fri Oct 08, 2010 3:47 am 
Newbie

Joined: Thu Mar 08, 2007 8:49 am
Posts: 11
Location: Hyd
Hi,
Place the exact code so that it will be helpful for debugging issue.
There is some difference in the column name used in POJO and Query.
Your property name is private Long sPid; in POJO.
In Named query a.sPSid= :spSid

Apart from above the code looks good. It hould not reutn duplicates.
One more thing make sure that your view is not returning any duplicates.

Thanks & Regards,
Sunil K

_________________
Thanks&Regards
Sunil k


Top
 Profile  
 
 Post subject: Re: Hibernate returns Duplicate Data
PostPosted: Fri Oct 08, 2010 8:49 am 
Senior
Senior

Joined: Fri Oct 08, 2010 8:44 am
Posts: 130
The reason is very simple. Your id column is not unique. Hibernate fetches IDs first and then thinks that all these objects are the same. Make sure you use unique column for your ID.


Top
 Profile  
 
 Post subject: Re: Hibernate returns Duplicate Data
PostPosted: Fri Oct 08, 2010 10:29 am 
Newbie

Joined: Wed Oct 06, 2010 12:49 pm
Posts: 8
Thanks again... Lesson learned on @Id!


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

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.