-->
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.  [ 2 posts ] 
Author Message
 Post subject: Are joins implicit in relationships?
PostPosted: Fri Apr 23, 2004 6:44 pm 
Newbie

Joined: Thu Apr 08, 2004 3:45 pm
Posts: 5
Location: Boulder, Colorado, USA
I've gotten confused about what I thought was implicit in Hibernate...

If I have a table of books with types:

Title BookType
About cats non-fiction
About rats non-fiction
About tats fiction

I then normalize it so that the types link to another table, type-table:

ID BookType
1 non-fiction
2 fiction

Then the book table is

Title BookType
About cats 1
About rats 1
About tats 2

I create classes

public class Books {
private String title;
private BookType type;
...
}

public class BookType {
private int id;
private String booktype;
...
}

I create an HBM mapping for Books with
<property
name="title"
type="string"
column="Title">
</property>

<many-to-one name="booktype"
class="model.Books"
column="category" />

and a mapping for BookType with
<property
name="id"
type="integer"
column="id">
</property>

<property
name="booktype"
type="string"
column="booktype">
</property>

Now, I am expecting that a Books object will automatically have the BookType filled in. Finding all books should do that:

List books = session.find("from Books");
System.out.println(books.get(0).getType()) should be "non-fiction".

I've confirmed that I can get that to work.

Now I'm confused about finding all of one type:

List fiction =session.find("from Books as book where book.type='fiction'")

I expected that the booktype would already be filled in which what it
refers to so that I could search on the text and not have to explicitly do a join. Isn't that how it works?

Do I have to do a join, or a where someting = something-else?

Hope this makes sense. The code may not be precise in my example. Sorry.

==Leonard

_________________
==Leonard


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 24, 2004 9:56 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You can only use the . navigation in querys for one ended associations. To do what you want use
Code:
select book from Books book join book.types type where type = 'xxx'


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