zhangjingl02 / activejdbc

Automatically exported from code.google.com/p/activejdbc
0 stars 0 forks source link

Model.parent() sts ID as Integer #178

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
this makes it impossible to test models using getId) because one model migth 
have Integer as ID, while the other Long

Original issue reported on code.google.com by i...@polevoy.org on 9 Oct 2012 at 3:34

GoogleCodeExporter commented 9 years ago
actually it is the long that is being set!

Original comment by i...@polevoy.org on 9 Oct 2012 at 4:13

GoogleCodeExporter commented 9 years ago

Original comment by i...@polevoy.org on 9 Oct 2012 at 4:13

GoogleCodeExporter commented 9 years ago
bad news, seems I cannot fix this. The reason is that the H2 and PostgreSQL 
work as expected, but MySQL Driver converts the ID for a table to Java Long, 
eve though it was declared in DML as INT. 
The culprit is 
http://code.google.com/p/activejdbc/source/browse/trunk/activejdbc/src/main/java
/org/javalite/activejdbc/DB.java#527

Converting this:
 long id = rs.getLong(1);
to:
 Object id = rs.getObject(1);

Fixes PostgreSQL and H2, but breaks MySQL. I have found no way to fix it for 
all databases. 

Test to recreate the problem:

public class Defect178Test extends ActiveJDBCTest {
    @Test
    public void shouldNotSetIdAsLongWhenCreatingRecord(){
        deleteFromTable("addresses");
        deleteFromTable("users");

        User u = User.createIt("first_name", "John", "last_name", "Doe", "email", "john@doe.com");
        User u1 = User.findFirst("first_name = ?", "John");
        a(u.getId().getClass()).shouldBeEqual(u1.getId().getClass());
    }
}

Original comment by i...@polevoy.org on 9 Oct 2012 at 4:35

GoogleCodeExporter commented 9 years ago

Original comment by i...@polevoy.org on 9 Oct 2012 at 4:35