-->
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.  [ 3 posts ] 
Author Message
 Post subject: Composite primary key query help needed
PostPosted: Tue Nov 16, 2004 4:45 pm 
Newbie

Joined: Tue Nov 16, 2004 3:54 pm
Posts: 5
Hi folks

I am trying to retrieve all the URLs for a given username.
I have three tables.
(1) url_links having (username, url_link) as a composite key
username is the foreign key to the register table.
(2) register where username is the primary key
What is a correct way of finding all the urls with given username?
My code below returns a null list.

Please provide a code snippet.

Thanks.

Yogi

Hibernate version: 2.1

Mapping documents:
<hibernate-mapping package="mydomain.persistence">
<class name="UrlLinks" table="url_links">
<composite-id class="UrlLinksPK" name="id">
<key-property
column="url_location"
name="urlLocation"
type="string"
/>
<key-many-to-one
class="Register"
column="username"
name="username"
/>
</composite-id>
<property
column="date_modified"
length="19"
name="dateModified"
not-null="false"
type="timestamp"
/>
-----
</hibernate-mapping >


Code between sessionFactory.openSession() and session.close():
sess = HibernateSession.currentSession();
Register r = new Register();
r.setUsername(username);
UrlLinksPK ulpk = new UrlLinksPK("", r);
UrlLinks ul = new UrlLinks();
ul.setId(ulpk);
List results = sess.createCriteria(UrlLinks.class)
.add( Example.create(ul) )
.list();
System.out.println(results.size());

Full stack trace of any exception that occurs:

Name and version of the database you are using:
MySQL 4.1

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 16, 2004 4:53 pm 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
From the reference manual chapter 12.6. Example queries:
Version properties, identifiers and associations are ignored.

HTH
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 17, 2004 3:03 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
Quote:
Ernst

Thanks for you reply to my post.

But my query does not really work as the document says. You think

Cat cat = new Cat();

cat.setSex('F');

cat.setColor(Color.BLACK);

List results = session.createCriteria(Cat.class)

.add( Example.create(cat) )

.list();

===================

would work for composite keys?



Thanks.



AFAIK yes.
Since identifiers are ignored for example queries it shouldn't matter what kind of primary key you're using.

HTH
Ernst


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