-->
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.  [ 4 posts ] 
Author Message
 Post subject: how to query in a linked table?
PostPosted: Mon Jul 21, 2008 5:44 am 
Newbie

Joined: Mon Jul 21, 2008 5:40 am
Posts: 16
Hi Gurus,

I have two tables namely Usr and Newbie, I'd like to return a list of usr from newbie, this works:

select usr from Newbie order by rand()

this works, now I'd like to return only those Usrs that has a valid avatar, here is the query:

select usr from Newbie where usr.avatar is not null order by rand()

this does not work it always has this exception, any idea? thanks


Caused by: org.hibernate.hql.ast.QuerySyntaxException: Invalid path: 'usr.avatar' [select usr from org.ust.entities.Newbie where usr.avatar is not null order by rand()]


public class Usr {

private Long id;
private Photo avatar;

@Id @GeneratedValue
public Long getId() {
return id;
}

@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name="photo_fk")

public Photo getAvatar() {
return avatar;
}

public void setAvatar(Photo avatar) {
this.avatar = avatar;
}
}

public class Newbie {

Long id;
private Usr usr;

@Id
@GeneratedValue
public Long getId() {
return id;
}

private void setId(Long id) {
this.id = id;
}

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name="usr_id")
public Usr getUsr() {
return usr;
}

public void setUsr(Usr usr) {
this.usr = usr;
}

}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 21, 2008 6:44 am 
Beginner
Beginner

Joined: Wed Sep 21, 2005 8:18 am
Posts: 31
Try this query:

select n.usr from Newbie n where n.usr.avatar is not null order by rand()

_________________
amer sohail


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 21, 2008 7:01 am 
Newbie

Joined: Mon Jul 21, 2008 5:40 am
Posts: 16
this works, thanks. but what's so difference between the two:

select n.usr from Newbie n where n.usr.avatar is not null order by rand()
select usr from Newbie where usr.avatar is not null order by rand()


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 21, 2008 7:25 am 
Beginner
Beginner

Joined: Wed Sep 21, 2005 8:18 am
Posts: 31
angelochen960 wrote:
this works, thanks. but what's so difference between the two:

select n.usr from Newbie n where n.usr.avatar is not null order by rand()
select usr from Newbie where usr.avatar is not null order by rand()


Difference is "usr.avatar". Hibernate does not recognize avatar in condition because that is a property of usr which you did not mention in from clause. Keep one thing in mind: condition clause only recognizes explicit declaration.

_________________
amer sohail


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