-->
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.  [ 9 posts ] 
Author Message
 Post subject: HQL Problem: Help!
PostPosted: Sun Sep 25, 2005 1:16 pm 
Beginner
Beginner

Joined: Sun Jul 31, 2005 1:23 pm
Posts: 35
This is with Hibernate 3.0.5

I have a pretty simple rating database. There are items to rate, users that rate items, and rating records:
Users: PK = Auto ID
Items: PK = Auto ID
Ratings: PK = Composite of two foreign keys (UserID, RatableItemID)

Here is the mapping hbm.xml:

Code:
<hibernate-mapping>
    <class name="com.auenrec.data.Rating" table="Ratings">
       <composite-id>
         <key-many-to-one name="user" column="UserID"
               class="com.auenrec.data.GenericUser"/>
         <key-many-to-one name="item" column="RatableItemID"
               class="com.auenrec.data.RatableItem"/>
      </composite-id>

        <property name="originalRating" column="OriginalRating" type="float"/>
        <property name="normalizedRating" column="NormalizedRating" type="float"/>
        <property name="ratingDate" column="RatingDate" type="timestamp"/>
        <property name="enabled" column="Enabled" type="boolean"/>
    </class>
</hibernate-mapping>

<hibernate-mapping>
    <class name="com.auenrec.data.GenericUser" table="GenericUsers">
        <id name="id" column="ID">
            <generator class="native"/>
        </id>
    </class>
</hibernate-mapping>


I want to query all Rating objects for a given user. I have tried:

Code:
from Rating WHERE user.id = ?



Code:
Hibernate: select rating0_.UserID as UserID, rating0_.RatableItemID as RatableI2_, rating0_.OriginalRating as Original3_5_, rating0_.NormalizedRating as Normaliz4_5_, rating0_.RatingDate as RatingDate5_, rating0_.Enabled as Enabled5_ from Ratings rating0_ where (rating0_.UserID, rating0_.RatableItemID)=? limit ?
SQL Error: 1241, SQLState: 21000
Operand should contain 2 column(s)


That SQL looks wrong. Why is it including RatableItemID in the WHERE clause?

I also have tried:

Code:
from Rating as ratung join rating.user as user WHERE user.id = ?


I get:

Code:
*** ERROR:  Invalid path: 'rating.user'
*** ERROR:  Invalid path: 'user.id'
*** ERROR: <AST>:0:0: unexpected end of subtree


I'm really lost as to what to do. I've done many searches and read through the docs and I can't figure out what I need to change. Please help!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 25, 2005 2:00 pm 
Pro
Pro

Joined: Fri Sep 02, 2005 4:21 am
Posts: 206
Location: Vienna
Try
Code:
from Rating r WHERE r.user.id = ?


Erik


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 25, 2005 2:21 pm 
Beginner
Beginner

Joined: Sun Jul 31, 2005 1:23 pm
Posts: 35
ErikFK wrote:
Try
Code:
from Rating r WHERE r.user.id = ?


Erik


Wow, thanks, that was fast! I think you fixed that issue but uncovered another problem. Now, I get the following exception on that query:

Code:
org.hibernate.PropertyAccessException thrown: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of com.auenrec.data.Rating.setEnabled


No idea what this means. I'm trying to figure out how to change that config setting in my hibernate.cfg.xml


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 25, 2005 2:41 pm 
Beginner
Beginner

Joined: Sun Jul 31, 2005 1:23 pm
Posts: 35
Why does this work:

Code:
from Rating rating WHERE rating.user.id = ?


and this doesn't:

Code:
from Rating WHERE Rating.user.id = ?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 25, 2005 2:59 pm 
Pro
Pro

Joined: Fri Sep 02, 2005 4:21 am
Posts: 206
Location: Vienna
LanceVance wrote:
Why does this work:

Code:
from Rating rating WHERE rating.user.id = ?


and this doesn't:

Code:
from Rating WHERE Rating.user.id = ?

I guess because of the way Hibernate is implemented... :-).

Concerning your other problem (CGLIB): I'd need to see the code of your Rating class, know the Hibernate and Java version you're using to be able to help you.

Erik


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 25, 2005 3:01 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
LanceVance wrote:
Why does this work:

Code:
from Rating rating WHERE rating.user.id = ?


and this doesn't:

Code:
from Rating WHERE Rating.user.id = ?


In the second query, Rating has no alias, not an alias of "Rating".


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 25, 2005 3:14 pm 
Beginner
Beginner

Joined: Sun Jul 31, 2005 1:23 pm
Posts: 35
gavin wrote:
LanceVance wrote:
Why does this work:

Code:
from Rating rating WHERE rating.user.id = ?


and this doesn't:

Code:
from Rating WHERE Rating.user.id = ?


In the second query, Rating has no alias, not an alias of "Rating".


Interesting: In SQL aliases are optional; you can use the original name or define an alias and use that. In HQL, aliases are required; which means it's more of a name mapping than an "alias".


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 25, 2005 3:17 pm 
Beginner
Beginner

Joined: Sun Jul 31, 2005 1:23 pm
Posts: 35
ErikFK wrote:
LanceVance wrote:
Why does this work:

Concerning your other problem (CGLIB): I'd need to see the code of your Rating class, know the Hibernate and Java version you're using to be able to help you.

Erik


Java: 1.5.0_5
Hibernate: 3.0.5

Hey, if you'd be willing to fix these newbie issues for a reasonable price, I'd be willing to deposit into a PayPal account.

Rating.java:
Code:
package com.auenrec.data;

import java.io.Serializable;
import java.util.Date;

import com.auenrec.core.HashUtility;

public class Rating implements Serializable {
   static final long serialVersionUID = 1;
   
   public boolean equals(Object other) {
      if (other instanceof Rating) {
         Rating otherRating = (Rating) other;
         
         return HashUtility.areObjectsEqual(this.user, otherRating.user)
            && HashUtility.areObjectsEqual(this.item, otherRating.item)
            && HashUtility.areObjectsEqual(this.ratingDate, otherRating.ratingDate)
            && HashUtility.areObjectsEqual(this.originalRating, otherRating.originalRating)
            && HashUtility.areObjectsEqual(this.normalizedRating, otherRating.normalizedRating);
      } else {
         return false;
      }
   }
   
   public int hashCode() {
      int result = HashUtility.HASH_SEED;
      result = HashUtility.combine(result, user);
      result = HashUtility.combine(result, item);
      result = HashUtility.combine(result, originalRating);
      result = HashUtility.combine(result, normalizedRating);
      return result;
   }
   
   private GenericUser user;
   private RatableItem item;
   
   private Float originalRating;
   private Float normalizedRating;   // aka Z rating
   private Date ratingDate = new Date();
   private boolean enabled = true;

   public GenericUser getUser() { return user; }
   public void setUser(GenericUser user) { this.user = user; }

   public RatableItem getItem() { return item; }
   public void setItem(RatableItem item) { this.item = item; }

   public Float getOriginalRating() { return originalRating; }
   public void setOriginalRating(Float originalRating) { this.originalRating = originalRating; }

   public Float getNormalizedRating() { return normalizedRating; }
   public void setNormalizedRating(Float normalizedRating) { this.normalizedRating = normalizedRating; }
   
   public Date getRatingDate() { return ratingDate; }
   public void setRatingDate(Date ratingDate) { this.ratingDate = ratingDate; }

   public boolean getEnabled() { return enabled; }
   public void setEnabled(boolean enabled) { this.enabled = enabled; }
}



Rating.hbm.xml:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
      "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.auenrec.data.Rating" table="Ratings">
       <composite-id>
         <key-many-to-one name="user" column="UserID"
               class="com.auenrec.data.GenericUser"/>
         <key-many-to-one name="item" column="RatableItemID"
               class="com.auenrec.data.RatableItem"/>
      </composite-id>

        <property name="originalRating" column="OriginalRating" type="float"/>
        <property name="normalizedRating" column="NormalizedRating" type="float"/>
        <property name="ratingDate" column="RatingDate" type="timestamp"/>
        <property name="enabled" column="Enabled" type="boolean"/>
    </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 25, 2005 3:17 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Right.

This is because we want people to stick to Java naming conventions: classnames initial uppercase, local variables initial lowercase.


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