tinyint sql type is converted to boolean

423 views
Skip to first unread message

Kyle

unread,
Jun 16, 2011, 4:09:10 PM6/16/11
to ActiveJDBC Group
The following test fails:

@Test
public void typeShouldBeInt(){
Group g = new Group();
g.set("type", 1);
a(g.get("type")).shouldBeType(int.class);
a(g.get("type")).shouldEqual(1);
}

Where the type column is defined as a TINYINT(1) in the mysql table.

Result:

Testcase: typeShouldBeInt took 0.015 sec
Caused an ERROR
class java.lang.Integer is not int
javalite.test.jspec.TestException: class java.lang.Integer is not int
at javalite.test.jspec.Expectation.shouldBeType(Expectation.java:90)
at idea.dal_tests.GroupSpec.typeShouldBeInt(GroupSpec.java:119)

Suggestions?

Igor Polevoy

unread,
Jun 16, 2011, 5:23:39 PM6/16/11
to activejd...@googlegroups.com
this is because your driver returns this as Integer

Do this:
               a(g.get("type")).shouldBeType(Integer.class);


thanks
Igor

Kyle Beyer

unread,
Jun 17, 2011, 9:20:53 AM6/17/11
to activejd...@googlegroups.com
It actually seems to think it is a boolean when retrieving from DB.  Although when I create the model in memory, it says Integer.  Check out the test and results below.

@Test
public void typeShouldAlwaysBeInteger(){
Group g = mockTestGroup();
g.set("type", 3);

// test type before save to db
a(g.get("type")).shouldBeType(Integer.class);
a(g.get("type")).shouldEqual(3);
g.saveIt();

// test type after loading from db
List<Group> groups = Group.findBySQL("SELECT * FROM groups WHERE groupName = '" + defaultName + "' LIMIT 1");
Group fromDb = groups.get(0);
a(fromDb.get("type")).shouldBeType(Integer.class); // LINE 126
a(fromDb.get("type")).shouldEqual(3);
// cleanup
fromDb.delete();
}

Results:

Testcase: typeShouldAlwaysBeInteger took 0.028 sec
Caused an ERROR
class java.lang.Boolean is not class java.lang.Integer
javalite.test.jspec.TestException: class java.lang.Boolean is not class java.lang.Integer
at javalite.test.jspec.Expectation.shouldBeType(Expectation.java:90)
at idea.dal_tests.GroupSpec.typeShouldAlwaysBeInteger(GroupSpec.java:126)

Kyle

unread,
Jun 17, 2011, 9:39:32 AM6/17/11
to ActiveJDBC Group
The big deal here is that there doesn't seem to be a workaround. When
I try to use getInteger("type") .... I get:

failed to convert: 'true' to Integer
javalite.common.ConversionException: failed to convert: 'true' to
Integer
at javalite.common.Convert.toInteger(Convert.java:221)
at activejdbc.Model.getInteger(Model.java:879)
at idea.dal.models.Group.getInteger(Group.java)
at idea.dal_tests.GroupSpec.typeShouldAlwaysBeInteger(GroupSpec.java:
127)

I saved it with value of 3. When I pull back out of the DB it is
'true'. What else should I try?

On Jun 17, 8:20 am, Kyle Beyer <k...@theideacenter.org> wrote:
> It actually seems to think it is a boolean when retrieving from DB.
>  Although when I create the model in memory, it says Integer.  Check out the
> test and results below.
>
> @Test
> public void typeShouldAlwaysBeInteger(){
> Group g = mockTestGroup();
> g.set("type", 3);
>
> // test type before save to db
> a(g.get("type")).shouldBeType(Integer.class);
> a(g.get("type")).shouldEqual(3);
> g.saveIt();
>
> // test type after loading from db
> List<Group> groups = Group.findBySQL("SELECT * FROM groups WHERE groupName =
> '" + defaultName + "' LIMIT 1");
> Group fromDb = groups.get(0);
> *a(fromDb.get("type")).shouldBeType(Integer.class); // LINE 126*
> a(fromDb.get("type")).shouldEqual(3);
>  // cleanup
> fromDb.delete();
>
> }
>
> Results:
>
> Testcase: typeShouldAlwaysBeInteger took 0.028 sec
> Caused an ERROR
> *class java.lang.Boolean is not class java.lang.Integer*
> javalite.test.jspec.TestException: class java.lang.Boolean is not class
> java.lang.Integer
> at javalite.test.jspec.Expectation.shouldBeType(Expectation.java:90)
> at idea.dal_tests.GroupSpec.typeShouldAlwaysBeInteger(*GroupSpec.java:126*)
>
>
>
>
>
>
>
> On Thu, Jun 16, 2011 at 4:23 PM, Igor Polevoy <i...@polevoy.org> wrote:
> > this is because your driver returns this as Integer
>
> > Do this:
> >                a(g.get("type")).shouldBeType(Integer.class);
>
> > thanks
> > Igor
>

Kyle

unread,
Jun 17, 2011, 10:15:54 AM6/17/11
to ActiveJDBC Group
FYI - I discovered the problem and it is with the driver as you
suggested. Here is a link to a thread that explains:
http://forums.mysql.com/read.php?39,412532,412768#msg-412768

Apparently prior to MySql 5.03 TINYINT(1) was used for BIT. So the
mysql connector assumed conversion to bool. Adding the param ?
tinyInt1isBit=false to your connection string will change the behavior
to treat TINYINT(1) as an integer.

ipolevoy

unread,
Jun 17, 2011, 12:47:05 PM6/17/11
to activejd...@googlegroups.com
ok, this makes sense. I also wrote a small page explaining AJ behavior when it comes with types, you can take a look here:
http://code.google.com/p/activejdbc/wiki/DataConversions

igor

ipolevoy

unread,
Jun 17, 2011, 12:49:41 PM6/17/11
to activejd...@googlegroups.com
AJ does not perform conversion in memory - what you stick into the model is what you are going to get.
Reply all
Reply to author
Forward
0 new messages