Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Query for boolean
Query for boolean [message #512861] Mon, 08 February 2010 08:43 Go to next message
Andre Kullmann is currently offline Andre KullmannFriend
Messages: 33
Registered: September 2009
Member
Hi,

how can I write a query with an boolean property in where clause ?

DDL
[...]
Loeschen	CHAR(1) CONSTRAINT CC_Pilot_Loeschen CHECK (Loeschen IN ('t','f'))	NOT NULL	DEFAULT 'f'

[...]

Java
[...]
@Column(nullable = false)
public Boolean getLoeschen() {
  return loeschen;
}

[...]

Query

Query q = tx.get().createQuery("select p from Pilot p where p.loeschen = :f");
q.setParameter( "f", Boolean.FALSE );


The resultList is always null Sad

Whats wrong ? Any ideas ?

Thanks,
André
Re: Query for boolean [message #512899 is a reply to message #512861] Mon, 08 February 2010 09:47 Go to previous messageGo to next message
Thomas Haskes is currently offline Thomas HaskesFriend
Messages: 147
Registered: July 2009
Senior Member
Hi Andre,

first, I don't think that Boolean-False equals the String 'f'. Th
problem is that you are mapping a java boolean property to a database
column that is of the type varchar2. We had the same probelm and used a
converter for this, because we had to reuse an old database shema that
wasn't aware of Booleans at all. With a converter you can advise EL to
convert a database value of 'f' to Boolean.false. this way you can
persist booleans as well, EL will write 'f' or 't' to the database.

The converter is used this way:

1. Define the converter.

@Entity
@Table(name = "XXXX")
@ObjectTypeConverter (
name="booleanConverter",
dataType=java.lang.String.class,
objectType=java.lang.Boolean.class,
conversionValues={
@ConversionValue(dataValue="T", objectValue="true"),
@ConversionValue(dataValue="F", objectValue="false")}

2. Use it at the particular columns where it is needed.

@Column(name = "FLAG")
@Convert("booleanConverter")
public Boolean getBoolean() {
return flag;
}

public void setBoolean(Boolean flag) {
this.flag = flag;
}

HTH,

Tom


Am 08.02.2010 09:43, schrieb Andre Kullmann:
> Hi,
>
> how can I write a query with an boolean property in where clause ?
>
> DDL
> [...]
> Loeschen CHAR(1) CONSTRAINT CC_Pilot_Loeschen CHECK (Loeschen IN
> ('t','f')) NOT NULL DEFAULT 'f'
> [...]
>
> Java
> [...]
>
> @Column(nullable = false)
> public Boolean getLoeschen() {
> return loeschen;
> }
>
> [...]
>
> Query
>
>
> Query q = tx.get().createQuery("select p from Pilot p where p.loeschen =
> :f");
> q.setParameter( "f", Boolean.FALSE );
>
>
> The resultList is always null :(
> Whats wrong ? Any ideas ?
>
> Thanks,
> André
Re: Query for boolean [message #512900 is a reply to message #512899] Mon, 08 February 2010 09:53 Go to previous messageGo to next message
Thomas Haskes is currently offline Thomas HaskesFriend
Messages: 147
Registered: July 2009
Senior Member
> problem is that you are mapping a java boolean property to a database
> column that is of the type varchar2.

Don't get confused, just saw you used Char. But the problem remains the
same. Should work using the converter though.
Re: Query for boolean [message #512988 is a reply to message #512900] Mon, 08 February 2010 14:01 Go to previous message
Andre Kullmann is currently offline Andre KullmannFriend
Messages: 33
Registered: September 2009
Member
Yeah that's it !!
Thanks !!

I add the @ObjectTypeConverter to my abstract base class for all entities. So I must declare the converter only once.

Smile
Previous Topic:stack overflow with subquery
Next Topic:@EntityListeners not inherited when TABLE_PER_CLASS inheritance strategy is used?
Goto Forum:
  


Current Time: Sat Apr 27 02:13:48 GMT 2024

Powered by FUDForum. Page generated in 0.03103 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top