-->
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: Querying Many-valued entity associations
PostPosted: Mon Nov 24, 2008 12:50 am 
Beginner
Beginner

Joined: Fri Jan 16, 2004 4:58 pm
Posts: 37
Hibernate version: 3.3.1

Code:
@Entity
public class Book extends BaseModel {   

    @NotNull
    private String title;
    private String description;

    @OneToMany(mappedBy = "book", cascade = CascadeType.ALL)
    private List<Chapter> chapters;

    @CollectionOfElements
    @JoinTable(name = "book_user", joinColumns = @JoinColumn(name = "book_id"))
    private List<BookUser> bookUsers;

    // getters and setters
}


Code:
@Entity
public class User extends BaseModel {

    @NotNull
    private String password;

    @NotNull
    private String firstName;

    @NotNull
    private String lastName;

    @NotNull
    private String emailAddress;

   // getters and setters
}


Code:
@Embeddable
public class BookUser {

    @Parent
    private Book book;

    @ManyToOne
    @JoinColumn(name = "user_id", nullable = false, updatable = false)
    private User user;

    @ManyToOne
    @JoinColumn(name = "role_id", nullable = true)
    private Role role;

   // getters and setters
}


I'm trying to find all books with a specific User ID. I am running the following query:

Code:
.createQuery("SELECT b from Book b JOIN b.bookUsers u where u.user.id=:userId")


I am getting the following exception:

Quote:
Caused by: org.hibernate.QueryException: could not resolve property: user.id of: com.publishgeek.dataAccess.model.Book [SELECT b from com.publishgeek.dataAccess.model.Book b left JOIN b.bookUsers u where u.user.id=:userId]
at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:44)
at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:38)
at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1362)
at org.hibernate.hql.ast.tree.FromElementType.getPropertyType(FromElementType.java:279)
at org.hibernate.hql.ast.tree.FromElement.getPropertyType(FromElement.java:386)
at org.hibernate.hql.ast.tree.DotNode.getDataType(DotNode.java:566)
at org.hibernate.hql.ast.tree.DotNode.prepareLhs(DotNode.java:241)
at org.hibernate.hql.ast.tree.DotNode.resolve(DotNode.java:188)




I'm using HSQLDB 1.8.0

Quote:
2008-11-23 22:38:30,959 QueryTranslatorImpl.java 266 DEBUG --- HQL AST ---
\-[QUERY] 'query'
+-[SELECT_FROM] 'SELECT_FROM'
| +-[FROM] 'from'
| | +-[RANGE] 'RANGE'
| | | +-[DOT] '.'
| | | | +-[DOT] '.'
| | | | | +-[DOT] '.'
| | | | | | +-[DOT] '.'
| | | | | | | +-[IDENT] 'com'
| | | | | | | \-[IDENT] 'publishgeek'
| | | | | | \-[IDENT] 'dataAccess'
| | | | | \-[IDENT] 'model'
| | | | \-[IDENT] 'Book'
| | | \-[ALIAS] 'b'
| | \-[JOIN] 'JOIN'
| | +-[LEFT] 'left'
| | +-[DOT] '.'
| | | +-[IDENT] 'b'
| | | \-[IDENT] 'bookUsers'
| | \-[ALIAS] 'u'
| \-[SELECT] 'SELECT'
| \-[IDENT] 'b'
\-[WHERE] 'where'
\-[EQ] '='
+-[DOT] '.'
| +-[DOT] '.'
| | +-[IDENT] 'u'
| | \-[IDENT] 'user'
| \-[IDENT] 'id'
\-[COLON] ':'
\-[IDENT] 'userId'

2008-11-23 22:38:30,959 ErrorCounter.java 68 DEBUG throwQueryException() : no errors


Thanks for any tips

_________________
Gregg Bolinger
Javaranch Sheriff
Weblog


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 24, 2008 3:22 pm 
Beginner
Beginner

Joined: Fri Jan 16, 2004 4:58 pm
Posts: 37
Thanks to a buddy of mine in Stripes IRC I got this resolved:

Code:
createQuery("SELECT b from Book b JOIN b.bookUsers u where u.user=:user")
[

_________________
Gregg Bolinger
Javaranch Sheriff
Weblog


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.