-->
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: composite key mapping
PostPosted: Fri Jun 13, 2008 1:41 pm 
Newbie

Joined: Fri Jun 13, 2008 1:24 pm
Posts: 1
Dear all,

I'm new to hibernate, I have create a mapping which have a composite key. mapping as follow,

<hibernate-mapping>
<class name="bean.AccountProfile" table="member_profiles">
<composite-id name="id" class="bean.AccountProfileId">
<key-property name="accountId" type="int" column="member_id" />
<key-property name="name" type="string" column="name" />
</composite-id>
<many-to-one name="account" class="bean.Account" update="false" insert="false" fetch="select">
<column name="member_id" not-null="true" />
</many-to-one>
<property name="value" type="string">
<column name="value" />
</property>
</class>
</hibernate-mapping>

what i want to do is to retrieve a list of AccountProfile objects filter by the accountId. following is my code,

accountProfile = new AccountProfile();
accountProfileId = new AccountProfileId();

accountProfileId.setAccountId(accountId);
accountProfile.setId(accountProfileId);

List accountProfiles = accountProfileDAO.findByExample(accountProfile);

method findByExample():

public List<AccountProfile> findByExample(AccountProfile instance) {
try {
sessionFactory.getCurrentSession().beginTransaction();
List<AccountProfile> results = List<AccountProfile>) sessionFactory.getCurrentSession().createCriteria("com.sysfotech.ums.bean.AccountProfile").add(create(instance)).list();
return results;
} catch (RuntimeException re) {
throw re;
}
}

the debug sql from hibernate,

Hibernate: select this_.member_id as member1_25_0_, this_.name as name25_0_, this_.value as value25_0_ from member_profiles this_ where (1=1)

I don't know why the sql's criteria is not the one which i have specify for the the accountId.

what i except is,

select this_.member_id as member1_25_0_, this_.name as name25_0_, this_.value as value25_0_ from member_profiles this_ where (member_id=?)

any ideas?? or i have missed something in my mapping file??

Thanks in advance.

Jeffery


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 16, 2008 8:51 am 
Beginner
Beginner

Joined: Tue Dec 12, 2006 6:43 am
Posts: 32
Location: London
User the criteria and specify the FetchMode.JOIN in the criteria.


List<AccountProfile> results = List<AccountProfile>) sessionFactory.getCurrentSession().createCriteria("com.sysfotech.ums.bean.AccountProfile").setFetchMode("accountProfiles",FetchMode.JOIN).add(Expression.eq("accountId", id)

_________________
Alan Mehio
London
UK


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 16, 2008 8:51 am 
Beginner
Beginner

Joined: Tue Dec 12, 2006 6:43 am
Posts: 32
Location: London
User the criteria and specify the FetchMode.JOIN in the criteria.


List<AccountProfile> results = List<AccountProfile>) sessionFactory.getCurrentSession().createCriteria("com.sysfotech.ums.bean.AccountProfile").setFetchMode("accountProfiles",FetchMode.JOIN).add(Expression.eq("accountId", id)

_________________
Alan Mehio
London
UK


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 16, 2008 8:56 am 
Beginner
Beginner

Joined: Tue Dec 12, 2006 6:43 am
Posts: 32
Location: London
User the criteria and specify the FetchMode.JOIN in the criteria.


List<AccountProfile> results = List<AccountProfile>) sessionFactory.getCurrentSession().createCriteria("com.sysfotech.ums.bean.AccountProfile").setFetchMode("accountProfiles",FetchMode.JOIN).add(Expression.eq("accountId", id)

_________________
Alan Mehio
London
UK


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 17, 2008 3:45 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Indeed, the Criteria API would be a great tool for doing these types of queries, although, I must say, I'm not sure if it needed to be said three times. :)

Here's a very simple tutorial on creating Hibernate3 Criteria Queries:


http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=09howtousethecriteriaapi

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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.