-->
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: Error mapping booleans
PostPosted: Mon Dec 29, 2003 6:20 pm 
Beginner
Beginner

Joined: Sat Dec 20, 2003 5:09 pm
Posts: 38
I'm almost there - I can persist everything but booleans. I've tried several different methods, none of which seem to work. The forum archives have a few references to this issue, but the resolution is to a create a userType, which doesn't seem right to me. I feel like I must be mis-configuring something obvious.

I've tried all of the following:

<property name="demo" column="demo" type="true_false"/>
<property name="demo" column="demo" type="boolean"/>
<property name="demo" column="demo" type="java.lang.Boolean"/>

In the bean, I've tried this

public boolean isDemo()
public void setDemo(boolean demo)

and the java Boolean class wrapeer

public Boolean isDemo()
public void setDemo(Boolean demo)

Error I get with postgreSQL version 7.3.4:

ERROR: pg_atoi: error in "f": can't parse "f"

It's like HIbernate is trying to save the string false instead of the boolean value false. Is my only recourse to try and figure out the a custom UserType and use that instead of the stock boolean?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 29, 2003 7:13 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
It should work using

Code:
<property name="demo" type="boolean" column="demo"/>

As Mapping, and a normal boolean as property type, and an integer column in the database.

At Least thats the way I do it :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 29, 2003 9:02 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Make sure you have property hibernate.query.substitutions set to

Code:
true 1, false 0, yes 'Y', no 'N'


somewhere in the config file. It instructs Hibernate to replace "true" in the query with "1", "false" with "0" etc


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 30, 2003 9:52 am 
Beginner
Beginner

Joined: Sat Dec 20, 2003 5:09 pm
Posts: 38
Hmmm, still not sure I understand what's going on. The datatype in my table is boolean, not int. If, at the postgresql command prompt, I show the table defn I see this:

demo | boolean | default false

so wouldn't specifying this:

<property name="dialect">net.sf.hibernate.dialect.PostgreSQLDialect</property>

in my hibernate.cfg.xml file take care of any conversion issues?

Anyway, I tried what dimas suggested and it still isn't working. In hibernate.cfg.xml I inserted this line:

<property name="hibernate.query.substitutions">true=1, false=0</property>

At start up, I see the property is being parsed from this log output:

30-12-03 08:35:08 DEBUG [main]: dialect=net.sf.hibernate.dialect.PostgreSQLDialect
30-12-03 08:35:08 DEBUG [main]: hibernate.query.substitutions=true=1, false=0

I've tried the mapping two different ways:

<property name="demo" column="demo" type="true_false"/>
<property name="demo" column="demo" type="boolean"/>

My java bean looks like this:

/***************************************************************************
* Returns the demo flag
**************************************************************************/
public boolean isDemo() { return this.demo; }

/***************************************************************************
* Sets the demo flag
**************************************************************************/
public void setDemo(boolean demo) { this.demo = demo; }

When I try to save the data I see it is trying to bind the string true to the parameter:

30-12-03 08:36:41 DEBUG [http8080-Processor25]: binding 'true' to parameter: 18


but then I get a similar error to before (with type set to true_false):

30-12-03 08:36:41 WARN [http8080-Processor25]: SQL Error: 0, SQLState: null
30-12-03 08:36:41 ERROR [http8080-Processor25]: ERROR: pg_atoi: error in "T": can't parse "T"

Error with type set to boolean:


30-12-03 08:50:12 WARN [http8080-Processor25]: SQL Error: 0, SQLState: null
30-12-03 08:50:12 ERROR [http8080-Processor25]: ERROR: pg_atoi: error in "t": can't parse "t"


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 30, 2003 9:54 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Please show the SQL hibernate is generating


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 30, 2003 9:57 am 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
Code:
        <property name="hibernate.query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 30, 2003 10:09 am 
Beginner
Beginner

Joined: Sat Dec 20, 2003 5:09 pm
Posts: 38
I'm unsure how to show the SQL after hibernate performs the subsititution. I have

<property name="show_sql">true</property>

And what I get in the logs is the SQL (pre-substitution):

Hibernate: select nextval ('mmseq')
Hibernate: insert into emailrcpts (cid, emp, demo) values (?, ?, ?)

HOWEVER, the configuration string dimas posted WORKED! Hurray! Thank you so much.

I must add that the support you guys have given is absolutely incredible! Again, thank you so much for you patience and extremely quick posts. Now I can finally get to work on my application logic.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 30, 2003 10:14 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Nice that you got this to work.

Just in case someone needs it, you can enable parameter logging by setting log4j.logger.net.sf.hibernate.type=debug in the log4j.properties.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 30, 2003 10:58 am 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
As someone noted on the forum earlier, this only works for Hibernate 2.0.x and doesn't work for 2.1 because because it does not have builtin statement cache (or something like that - just search forum for parameter name)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 30, 2003 3:39 pm 
Beginner
Beginner

Joined: Sat Dec 20, 2003 5:09 pm
Posts: 38
I have to come clean and display my idiocy to the hibernate world. I'm posting this so someone else with the same issue doesn't get side-tracked with miss-information.

Anyway, after digging through my examples more I realized that specifying this:

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

Isn't doing what I thought it was doing. In fact, I'm not sure its even working. If I try and add boolean values to my postgreSQL database manually via psql I am reminded that postgreSQL is extremely tolerant in what you can specify as a boolean. For example, I can use true (without qoutes), or 'true' (with qoutes):

prospect=# insert into emailrcpts (cid, emp, demo) values (1234, 12345, true);
INSERT 17837 1
prospect=# insert into emailrcpts (cid, emp, demo) values (1234, 12345, 'true');
INSERT 17838 1

But what I CAN'T do is use an integer value:


prospect=# insert into emailrcpts (cid, emp, demo) values (1234, 12345, 1);
ERROR: column "demo" is of type boolean but expression is of type integer
You will need to rewrite or cast the expression
ERROR: column "demo" is of type boolean but expression is of type integer
You will need to rewrite or cast the expression
prospect=#


And it is my understanding that the substition property was supposed to do just that - replace true with 1, false with 0.


End result is that the reason I was having problems is because one of the fields I was trying to store as boolean, was actually and int in the database! Hibernate works correctly out of the box with postgresql booleans. Nothing fancy requried.

Sheesh. How embarrassing. Dimas, Gloeglm sorry to put you through this exercise.

Rich


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.