-->
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.  [ 10 posts ] 
Author Message
 Post subject: Problem with HQL query regarding Booleans
PostPosted: Thu Jan 08, 2004 6:05 pm 
Regular
Regular

Joined: Tue Jan 06, 2004 3:32 pm
Posts: 80
Location: Munich, Germany
Can anyone tell me whats wrong with the following HQL?

FROM test.UserOTK AS otk WHERE (otk.name = 'email' OR otk.name = 'xmpp') AND NOT otk.used

otk.used is a Boolean and if I leave out "AND NOT otk.used", it works.

Here is the Exception I get:

Incorrect query syntax [FROM test.UserOTK AS otk WHERE (otk.name = 'email' OR otk.name = 'xmpp') AND NOT otk.used]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 6:47 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
you should use

AND otk.used=false


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 6:54 pm 
Regular
Regular

Joined: Tue Jan 06, 2004 3:32 pm
Posts: 80
Location: Munich, Germany
Tried that, now I get

Column not found, message from server: "Unknown column 'false' in 'where clause'"

Here is the complete SQL statement that is passed to MySQL:

select userotk0_.id as id, userotk0_.`key` as y2_, userotk0_.user_name as user_name, user otk0_.attribute as attribute, userotk0_.value as value, userotk0_.used as used, userotk0_.created_at as created_at, userotk0_.valid_until as valid_un8_, userotk0_.last_reminder_at as last_rem9_ from user_one_time_keys userotk0_ where ((userotk0_.attribute='email' )OR(usero
tk0_.attribute='xmpp' ))AND(userotk0_.used=false )


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 8:23 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Please show the class and the mapping.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 09, 2004 3:19 am 
Regular
Regular

Joined: Tue Jan 06, 2004 3:32 pm
Posts: 80
Location: Munich, Germany
/**
* @hibernate.class table="user_one_time_keys"
* @hibernate.cache usage="nonstrict-read-write"
*/
public class UserOTK
{
private int id;
private String key;
private User user;
private String name;
private String value;
private boolean used;
private Date createdAt;
private Date validUntil;
private Date lastReminderAt;

public UserOTK()
{
}

public UserOTK(String name, String value) throws HibernateException
{
setKey(generateKey());
setName(name);
setValue(value);
setCreatedAt(new Date());
}

/**
* @hibernate.id column="id" type="integer" generator-class="identity"
*/
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}

/**
* @hibernate.property column="`key`" type="string"
*/
public String getKey()
{
return key;
}
public void setKey(String key)
{
this.key = key;
}

/**
* @hibernate.many-to-one column="user_name" type="de.schildbach.user.business.User" not-null="false" outer-join="false"
*/
public User getUser()
{
return user;
}
public void setUser(User user)
{
this.user = user;
}

/**
* @hibernate.property column="attribute" type="string"
*/
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}

/**
* @hibernate.property column="value" type="string"
*/
public String getValue()
{
return value;
}
public void setValue(String value)
{
this.value = value;
}

/**
* @hibernate.property column="used" type="boolean"
*/
public boolean isUsed()
{
return used;
}
public void setUsed(boolean used)
{
this.used = used;
}

/**
* @hibernate.property column="created_at" type="timestamp"
*/
public Date getCreatedAt()
{
return createdAt;
}
public void setCreatedAt(Date createdAt)
{
this.createdAt = createdAt;
}

/**
* @hibernate.property column="valid_until" type="timestamp"
*/
public Date getValidUntil()
{
return validUntil;
}
public void setValidUntil(Date validUntil)
{
this.validUntil = validUntil;
}

/**
* @hibernate.property column="last_reminder_at" type="timestamp"
*/
public Date getLastReminderAt()
{
return lastReminderAt;
}
public void setLastReminderAt(Date lastReminderAt)
{
this.lastReminderAt = lastReminderAt;
}

...
}

<hibernate-mapping>
<class
name="de.schildbach.user.business.UserOTK"
table="user_one_time_keys"
dynamic-update="false"
dynamic-insert="false"
>
<cache usage="nonstrict-read-write" />

<id
name="id"
column="id"
type="integer"
>
<generator class="identity">
</generator>
</id>

<property
name="key"
type="string"
update="true"
insert="true"
column="`key`"
/>

<many-to-one
name="user"
class="de.schildbach.user.business.User"
cascade="none"
outer-join="false"
update="true"
insert="true"
column="user_name"
not-null="false"
/>

<property
name="name"
type="string"
update="true"
insert="true"
column="attribute"
/>

<property
name="value"
type="string"
update="true"
insert="true"
column="value"
/>

<property
name="used"
type="boolean"
update="true"
insert="true"
column="used"
/>

<property
name="createdAt"
type="timestamp"
update="true"
insert="true"
column="created_at"
/>

<property
name="validUntil"
type="timestamp"
update="true"
insert="true"
column="valid_until"
/>

<property
name="lastReminderAt"
type="timestamp"
update="true"
insert="true"
column="last_reminder_at"
/>

</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 09, 2004 7:57 am 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
make sure you put

<property name="hibernate.query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>


in your configuration XML


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 09, 2004 8:04 am 
Beginner
Beginner

Joined: Sat Oct 18, 2003 10:19 am
Posts: 21
Location: Belgium
I usually use a named parameter and assign Boolean(true) to it...


" .... where otk.used = :used"

q.setParameter("used", new Boolean(true), Hibernate.BOOLEAN);

_________________
Koen Maes
Leuven, Belgium


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 09, 2004 8:07 am 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
This is not needed really.
The problem here is "false" which is not a HQL keyword. To make it work the only thing you need is to tell Hibernate to replace it with zero. That is what configuration line I pasted above does.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 09, 2004 8:35 am 
Regular
Regular

Joined: Tue Jan 06, 2004 3:32 pm
Posts: 80
Location: Munich, Germany
This should maybe mentioned in the HQL section of the Hibernate Documentation.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 09, 2004 8:55 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Done.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


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