-->
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.  [ 5 posts ] 
Author Message
 Post subject: Independently Populate Class & subclass - Duplicate Reco
PostPosted: Thu Nov 02, 2006 8:19 pm 
Newbie

Joined: Thu Nov 02, 2006 7:35 pm
Posts: 4
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3.1

Mapping documents:

<class name="app.bo.CompanyLight"
table="company">

<!-- Primary Key -->
<id name="company_id"
column="company_id">
<generator class="increment"/>
</id>

<!-- Direct Data Properties -->
<property name="name"
column="name"/>
<property name="description"
column="description"/>


</class>


<class name="app.bo.Company"
table="company">

<!-- Primary Key -->
<id name="company_id"
column="company_id">
<generator class="increment"/>
</id>

<!-- Direct Data Properties -->
<property name="name"
column="name"/>
<property name="description"
column="description"/>


<!-- Relationship References -->

<component name="latest_numbers" class="app.set.LatestDataSet">
<set name="psetVal" lazy="false" cascade="save-update">
<key column="company_id"/>
<one-to-many class="app.bo.LatestData"/>
</set>
</component>




Code between sessionFactory.openSession() and session.close():
Session session =
HibernateUtil.getSessionFactory().openSession();
// if(!session.isOpen())
// session = HibernateUtil.getSessionFactory().openSession();
//session.setCacheMode(CacheMode.REFRESH);
Transaction tx = session.beginTransaction();
Query q = session.createQuery("from CompanyLight");
personList = (List) q.list();


for(int i=0;i<personList.size();i++)
{
CompanyLight p = (CompanyLight)personList.get(i);
System.out.println(p.getCompany_id() + p.getDescription());
}



Full stack trace of any exception that occurs:

Name and version of the database you are using:MySQL

The generated SQL (show_sql=true): Hibernate: select company0_.company_id as company1_0_, company0_.name as name0_, company0_.description as descript3_0_ from company company0_
Hibernate: select psetval0_.company_id as company5_1_, psetval0_.latest_number_id as latest1_1_, psetval0_.latest_number_id as latest1_1_0_, psetval0_.number_value as number2_1_0_, psetval0_.text_value as text3_1_0_, psetval0_.datatype_id as datatype4_1_0_, psetval0_.company_id as company5_1_0_ from latest_data psetval0_ where psetval0_.company_id=?
Hibernate: select psetval0_.company_id as company5_1_, psetval0_.latest_number_id as latest1_1_, psetval0_.latest_number_id as latest1_1_0_, psetval0_.number_value as number2_1_0_, psetval0_.text_value as text3_1_0_, psetval0_.datatype_id as datatype4_1_0_, psetval0_.company_id as company5_1_0_ from latest_data psetval0_ where psetval0_.company_id=?
Hibernate: select psetval0_.company_id as company5_1_, psetval0_.latest_number_id as latest1_1_, psetval0_.latest_number_id as latest1_1_0_, psetval0_.number_value as number2_1_0_, psetval0_.text_value as text3_1_0_, psetval0_.datatype_id as datatype4_1_0_, psetval0_.company_id as company5_1_0_ from latest_data psetval0_ where psetval0_.company_id=?
Hibernate: select psetval0_.company_id as company5_1_, psetval0_.latest_number_id as latest1_1_, psetval0_.latest_number_id as latest1_1_0_, psetval0_.number_value as number2_1_0_, psetval0_.text_value as text3_1_0_, psetval0_.datatype_id as datatype4_1_0_, psetval0_.company_id as company5_1_0_ from latest_data psetval0_ where psetval0_.company_id=?
Hibernate: select psetval0_.company_id as company5_1_, psetval0_.latest_number_id as latest1_1_, psetval0_.latest_number_id as latest1_1_0_, psetval0_.number_value as number2_1_0_, psetval0_.text_value as text3_1_0_, psetval0_.datatype_id as datatype4_1_0_, psetval0_.company_id as company5_1_0_ from latest_data psetval0_ where psetval0_.company_id=?
Hibernate: select psetval0_.company_id as company5_1_, psetval0_.latest_number_id as latest1_1_, psetval0_.latest_number_id as latest1_1_0_, psetval0_.number_value as number2_1_0_, psetval0_.text_value as text3_1_0_, psetval0_.datatype_id as datatype4_1_0_, psetval0_.company_id as company5_1_0_ from latest_data psetval0_ where psetval0_.company_id=?
Hibernate: select psetval0_.company_id as company5_1_, psetval0_.latest_number_id as latest1_1_, psetval0_.latest_number_id as latest1_1_0_, psetval0_.number_value as number2_1_0_, psetval0_.text_value as text3_1_0_, psetval0_.datatype_id as datatype4_1_0_, psetval0_.company_id as company5_1_0_ from latest_data psetval0_ where psetval0_.company_id=?
Hibernate: select psetval0_.company_id as company5_1_, psetval0_.latest_number_id as latest1_1_, psetval0_.latest_number_id as latest1_1_0_, psetval0_.number_value as number2_1_0_, psetval0_.text_value as text3_1_0_, psetval0_.datatype_id as datatype4_1_0_, psetval0_.company_id as company5_1_0_ from latest_data psetval0_ where psetval0_.company_id=?
Hibernate: select psetval0_.company_id as company5_1_, psetval0_.latest_number_id as latest1_1_, psetval0_.latest_number_id as latest1_1_0_, psetval0_.number_value as number2_1_0_, psetval0_.text_value as text3_1_0_, psetval0_.datatype_id as datatype4_1_0_, psetval0_.company_id as company5_1_0_ from latest_data psetval0_ where psetval0_.company_id=?
Hibernate: select psetval0_.company_id as company5_1_, psetval0_.latest_number_id as latest1_1_, psetval0_.latest_number_id as latest1_1_0_, psetval0_.number_value as number2_1_0_, psetval0_.text_value as text3_1_0_, psetval0_.datatype_id as datatype4_1_0_, psetval0_.company_id as company5_1_0_ from latest_data psetval0_ where psetval0_.company_id=?
Hibernate: select psetval0_.company_id as company5_1_, psetval0_.latest_number_id as latest1_1_, psetval0_.latest_number_id as latest1_1_0_, psetval0_.number_value as number2_1_0_, psetval0_.text_value as text3_1_0_, psetval0_.datatype_id as datatype4_1_0_, psetval0_.company_id as company5_1_0_ from latest_data psetval0_ where psetval0_.company_id=?
Hibernate: select companylig0_.company_id as company1_0_, companylig0_.name as name0_, companylig0_.description as descript3_0_ from company companylig0_


1great company
2superb place
3All Inovation
5divine
6divine
7dfghdfgh
8haroon
9Dello
10google
11harommmbkkk ioio
12good energy nnn mm kkoopp
1great company
2superb place
3All Inovation
5divine
6divine
7dfghdfgh
8haroon
9Dello
10google
11harommmbkkk ioio
12good energy nnn mm kkoopp

Records are appearing twice!




I have classes in the following structure:

class a {
int x;
int y;
}

class b extends class a{
Set z;
}

I want to populate a list with class a only first, and then based on my need populate class b. This is similar to lazy loading, but I want to deliberately split it out between two classes, to satisfy the framework i'm using.

For this reason i've to specify 2 mapping files, both referring to the same table, since class b also needs the table used by class a.

The problem I'm runing into is that if I populate just class a, the result set I'm getting is doubled (duplicate records for each row). If I populate Class b, then everything is fine, but I want the collection of class a, not class b.

I'm sure Hibernate has the technology to solve this problem, but I'm struggling to find a solution.

Thanks!

Haroon


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 03, 2006 6:02 am 
Regular
Regular

Joined: Tue May 16, 2006 3:32 am
Posts: 117
Set polymorphism="explicit" in your hbm mapping for CompanyLight at class level.


From Hibernate reference doc:

"Implicit polymorphism means that instances of the class will be returned by a query that names any superclass or implemented interface or the class and that instances of any subclass of the class will be returned by a query that names the class itself. Explicit polymorphism means that class instances will be returned only by queries that explicitly name that class and that queries that name the class will return only instances of subclasses mapped inside this <class> declaration as a <subclass> or <joined-subclass>. For most purposes the default, polymorphism="implicit", is appropriate. Explicit polymorphism is useful when two different classes are mapped to the same table (this allows a "lightweight" class that contains a subset of the table columns). "


Top
 Profile  
 
 Post subject: Thanks, that worked!
PostPosted: Fri Nov 03, 2006 4:35 pm 
Newbie

Joined: Thu Nov 02, 2006 7:35 pm
Posts: 4
That worked with a slight change. I added the polymorphism="explicit" in the class tag for Company, and not CompanyLight.

But is'nt that a bug with hibernate? What is the reason for getting duplicate rows, without that change?

But thanks a lot! You renewed my trust in hibernate!

Haroon


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 06, 2006 1:30 am 
Regular
Regular

Joined: Tue May 16, 2006 3:32 am
Posts: 117
No, it is not a bug.

You could read about polymorphic queries:

http://www.hibernate.org/hib_docs/v3/re ... lymorphism


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 10, 2006 11:38 am 
Newbie

Joined: Thu Nov 02, 2006 7:35 pm
Posts: 4
Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.