-->
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.  [ 7 posts ] 
Author Message
 Post subject: Polymorphic query could not resolve property: class of
PostPosted: Thu Mar 09, 2006 6:17 am 
Newbie

Joined: Tue Mar 07, 2006 10:51 am
Posts: 4
Hi,

I have added an object-model structure which consist of 7 diffrent class-types to hibernate.
After putting in some data I wanted to find out which object has been updated since a given timestamp.
Every class has the property "serverTimeStamp" which is a long.
So I started out with a polymorphic query like this:

session.createQuery("from java.lang.Object obj where obj.serverTimeStamp>" + sinceTimeStamp).list();

This works fine, and I get all objects modified since the timestamp.

Then I realized that I want skip one of the class-types called Journey, and tried this:

session.createQuery("from java.lang.Object obj where obj.class != Journey AND obj.serverTimeStamp>" + sinceTimeStamp).list();

Then an exception occured:

org.hibernate.QueryException: could not resolve property: class of: com.Position [from com.Position obj where obj.class != Journey AND obj.serverTimeStamp>0]


I took one step back and tried this:

List res = session.createQuery("from java.lang.Object obj where obj.class = Journey");

But still the same exception:

org.hibernate.QueryException: could not resolve property: class of: com.Position [from com.Position obj where obj.class = Journey]

It seems like hibernate is not able to figure out which class com.Position is. This is strange as com.Position is defined in the mapping file.
I have searched the documentation and forums but I can't figure out what's wrong.
Yes, I know I can do 6 separate queries to do the same job:
session.createQuery("from Driver obj where obj.serverTimeStamp>" + sinceTimeStamp).list();
.
.
.

But from the documentation is seems that my query should be possible, and it's much more elegant than 6 separate queries.
None of these 7 classes are subClasses of another of these 7 classes.
Is there some special requirement to the mapping files to make this work ?
Has anyone been able to do this type of polymorphic query ?

I use hibernate 3.1 and I consider myself as a beginner in the Hibernate context.

Thanks in advance.
Reg.
BT


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 09, 2006 9:23 am 
Regular
Regular

Joined: Tue Mar 07, 2006 11:18 am
Posts: 54
Location: Berlin
This is from the Hibernate reference:

Quote:
Likewise, the special property class accesses the discriminator value of an instance in the case of polymorphic persistence. A Java class name embedded in the where clause will be translated to its discriminator value.

....

Quote:
from AuditLog log, Payment payment
where log.item.class = 'Payment' and log.item.id = payment.id

Notice that log.item.class and payment.class would refer to the values of completely different database columns in the above query.



simon


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 09, 2006 10:23 am 
Newbie

Joined: Tue Mar 07, 2006 10:51 am
Posts: 4
Simon,
Yes, I have read this, but this is about checking the classtype of an object log has an association to.
I want to check the class-type of the main object itself (in your example, log).

I have now tried to run a similar query like this on my database, and I get the same exception.


More ideas ?

BT


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 09, 2006 10:54 am 
Regular
Regular

Joined: Tue Mar 07, 2006 11:18 am
Posts: 54
Location: Berlin
bitman wrote:
Simon,
Yes, I have read this, but this is about checking the classtype of an object log has an association to.
I want to check the class-type of the main object itself (in your example, log).

I have now tried to run a similar query like this on my database, and I get the same exception.


More ideas ?

BT


As I wrote the class name in a where clause will be translated to the discriminator value which is use to map polymorph classes. do you have a discriminator value in your mappings / tables?

I guess otherwise your approach won't work.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 10, 2006 4:22 am 
Newbie

Joined: Tue Mar 07, 2006 10:51 am
Posts: 4
The documentation states that this is an optional field, and it defaults to the className, so it should not be necessary to add it (as long as I'am able to spell the classname correct in the query :) ).

discriminator-value (optional - defaults to the class name): A value that distiguishes individual subclasses, used for polymorphic behaviour. Acceptable values include null and not null.

Just to be sure:
I tried to add an discriminator value, but the same exception occurs.

Anyway, thanks Simon.


Top
 Profile  
 
 Post subject: What type of polymorphic mapping do you use?
PostPosted: Fri Mar 10, 2006 10:58 am 
Beginner
Beginner

Joined: Mon Mar 14, 2005 6:07 pm
Posts: 36
Do you map using <subclass> or <joined-subclass> ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 14, 2006 4:00 am 
Newbie

Joined: Tue Mar 07, 2006 10:51 am
Posts: 4
There is no subclasses in use here (The only object they extend is java.lang.Object).

Here is the Journey class:

Code:
package com;

public class Journey
{
   private long id;
   private String Name;
   private long serverTimeStamp;

   private Location departureLocation;

   public Journey()
   {
   }

   public long getId()
   {
      return id;
   }

   public void setId(long id)
   {
      this.id = id;
   }

   public String getName()
   {
      return Name;
   }

   public void setName(String name)
   {
      Name = name;
   }

   public Location getDepartureLocation()
   {
      return departureLocation;
   }

   public void setDepartureLocation(Location departureLocation)
   {
      this.departureLocation = departureLocation;
   }

   public long getServerTimeStamp()
   {
      return serverTimeStamp;
   }

   public void setServerTimeStamp(long serverTimeStamp)
   {
      this.serverTimeStamp = serverTimeStamp;
   }
}



And here is the Location class:

Code:
package com;

public class Location
{
   private long id;
   private String Name;
   private long serverTimeStamp;

   public Location()
   {
   }

   public long getId()
   {
      return id;
   }

   public void setId(long id)
   {
      this.id = id;
   }

   public String getName()
   {
      return Name;
   }

   public void setName(String name)
   {
      Name = name;
   }

   public long getServerTimeStamp()
   {
      return serverTimeStamp;
   }

   public void setServerTimeStamp(long serverTimeStamp)
   {
      this.serverTimeStamp = serverTimeStamp;
   }
}



And here is the mapping for these two classes:

Code:
<hibernate-mapping>
   <class name="com.Journey" table="JOURNEY">
      <id name="id" column="JOURNEY_ID" type="long">
      </id>
     <property name="name" />
     <property name="serverTimeStamp"/>
     <many-to-one name="departureLocation" class="com.Location" cascade="persist" />
  </class>

  <class name="com.Location" table="LOCATION">
      <id name="id" column="LOCATION_ID" type="long">
      </id>
     <property name="name" />
     <property name="serverTimeStamp"/>
  </class>
</hibernate-mapping>


And as I said both these queries fails:

session.createQuery("from java.lang.Object obj where obj.class=Journey")
session.createQuery("from java.lang.Object obj where obj.class='com.Journey'")


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.