Discussion:
MYSQL Boolean: True, False and Null
mtreen
2009-09-08 17:16:47 UTC
Permalink
This is probably a simple question. I've looked everywhere I can think of for the answer:



A Mysql column that is defined as boolean or tinyint(1) can only have 2 values, 0 or 1. This column is bound to a checkbox.



When a new record is created in a toplink environment and that record contains a boolean field, the only way I can see to prevent a null exception is to modify the generated entity and have each boolean column explicitly look for null and then set the boolean to false:



public Boolean getReportselection() {

if (reportselection == null) reportselection = false;

return reportselection;

}



If I do not include the if (.....) .... = false in the entity then the form creation fails.



Showing my entity ignorance, is there any way to automatically have the boolean nulls return a false or does each field have to be explicitly dealt with?

------------------------
Lost in North Africa
Florian Brunner
2009-09-08 17:30:09 UTC
Permalink
If a property cannot be null, you could use a primitive type instead of
the wrapper and specify something like @Column(nullable=false)

http://java.sun.com/javaee/5/docs/api/javax/persistence/Column.html

-Florian
Post by mtreen
A Mysql column that is defined as boolean or tinyint(1) can only have 2 values, 0 or 1. This column is bound to a checkbox.
public Boolean getReportselection() {
if (reportselection == null) reportselection = false;
return reportselection;
}
If I do not include the if (.....) .... = false in the entity then the form creation fails.
Showing my entity ignorance, is there any way to automatically have the boolean nulls return a false or does each field have to be explicitly dealt with?
------------------------
Lost in North Africa
Rick
2009-09-08 19:57:12 UTC
Permalink
With MySql use a default value, the DDL for a field called "btest" in a
table called "dummy" looks like:

CREATE TABLE dummy (
btest Boolean NOT NULL DEFAULT 0
)

Every new record created will have the boolean field set to 0 unless you
explicitly set it to 1.

Rick
Post by Florian Brunner
If a property cannot be null, you could use a primitive type instead
http://java.sun.com/javaee/5/docs/api/javax/persistence/Column.html
-Florian
Post by mtreen
This is probably a simple question. I've looked everywhere I can
A Mysql column that is defined as boolean or tinyint(1) can only have
2 values, 0 or 1. This column is bound to a checkbox.
When a new record is created in a toplink environment and that record
contains a boolean field, the only way I can see to prevent a null
exception is to modify the generated entity and have each boolean
public Boolean getReportselection() {
if (reportselection == null) reportselection = false;
return reportselection;
}
If I do not include the if (.....) .... = false in the entity then
the form creation fails.
Showing my entity ignorance, is there any way to automatically have
the boolean nulls return a false or does each field have to be
explicitly dealt with?
------------------------
Lost in North Africa
mtreen
2009-09-08 21:28:21 UTC
Permalink
Thanks for the reply.



Works well.



Is there an easy way to apply this to ALL generated boolean columns or do I have to apply to each one?

------------------------
Lost in North Africa
Futaleufu_John
2009-09-09 11:37:02 UTC
Permalink
When you define your MySQL table, you can define a Boolean column to be
non-null and to have a default value. Indeed, you can do this with any data
type.

Then when you create an entity bean from that table, the generated code
should reflect the default value.

For example, suppose you define the following column in a table:

is_active TINYINT(1) NOT NULL DEFAULT 1

When you create the entity bean from this table, the column translates to:

@Column(name = "is_active", nullable = false)
private Boolean isActive = Boolean.TRUE;
Post by mtreen
Is there an easy way to apply this to ALL generated boolean columns or do
I have to apply to each one?
-----
Any ads or links to ads that appear in this post are not endorsed nor
recommended by this poster.
--
View this message in context: http://www.nabble.com/MYSQL-Boolean%3A-True%2C-False-and-Null-tp25350490p25363125.html
Sent from the Netbeans IDE Users mailing list archive at Nabble.com.
Rick
2009-09-08 23:05:40 UTC
Permalink
You can use ALTER, MODIFY statements but I don't know if there is a way
to search for columns based on the datatype.
Post by mtreen
Thanks for the reply.
Works well.
Is there an easy way to apply this to ALL generated boolean columns or do I have to apply to each one?
------------------------
Lost in North Africa
Gregg Wonderly
2009-09-10 15:31:13 UTC
Permalink
Post by Rick
You can use ALTER, MODIFY statements but I don't know if there is a way
to search for columns based on the datatype.
You can use the meta data access classes in JDBC to go through a database, table
by table and column by column to see what's there, and then generate SQL
statements to execute based on what you find. I do this to "dump" databases for
export into other SQL server technologies.

Gregg Wonderly
Post by Rick
Post by mtreen
Thanks for the reply.
Works well.
Is there an easy way to apply this to ALL generated boolean columns or
do I have to apply to each one?
------------------------
Lost in North Africa
Rick
2009-09-10 19:47:04 UTC
Permalink
Hi Gregg,

Great idea, I was thinking in SQL terms only, but that's a great way to
do it.

Rick
Post by Gregg Wonderly
Post by Rick
You can use ALTER, MODIFY statements but I don't know if there is a
way to search for columns based on the datatype.
You can use the meta data access classes in JDBC to go through a
database, table by table and column by column to see what's there, and
then generate SQL statements to execute based on what you find. I do
this to "dump" databases for export into other SQL server technologies.
Gregg Wonderly
Post by Rick
Post by mtreen
Thanks for the reply.
Works well.
Is there an easy way to apply this to ALL generated boolean columns
or do I have to apply to each one?
------------------------
Lost in North Africa
mtreen
2009-09-10 12:43:01 UTC
Permalink
Thanks, everyone!



I changed the database definitions to not null with a default value and it works like a charm. I could have sworn I did this before but I've twisted this thing so many ways, I may have missed this.



It usually goes back to a simple solution.



Thanks, again

------------------------
Lost in North Africa
Loading...