Java – Use the Sugar ORM with UUID or Joda-Time

Use the Sugar ORM with UUID or Joda-Time… here is a solution to the problem.

Use the Sugar ORM with UUID or Joda-Time

I’m creating a mobile app whose users are defined as follows:

public class UserObject extends SugarRecord<UserObject>
{
    public UUID Id;
    public String Name;
    public int DefaultSort;
    public String Username;
    public Date LastLogin;

public UserObject()
    {

}

public UserObject(UUID id, String name, int defaultSort, String username, String password, Date lastLogin)
    {
        this. Id = id;
        this. Name = name;
        this. DefaultSort = defaultSort;
        this. Username = username;
        this. LastLogin = lastLogin;
    }
}

These users will retrieve from the API – the ID is stored in the database as a unique identifier (or GUID in C#).

When I enter a new record in the user table, everything works fine :

    DateTime dt = new DateTime();
    UUID newUID = UUID.randomUUID();
    UserObject newUser = new UserObject(newUID, "David", 1, "userOne", "password", dt.toDate());
    newUser.save();

However, I get an error when I try to get the value back:

Class cannot be read from Sqlite3 database. Please check the type of field Id(java.util.UUID)

Does Sugar ORM (or SQLite) not support UUID at all? If I try to use Joda DateTime, replacing “LastLogin”, the table doesn’t build at all, so it looks as if fields can be created, just doesn’t retrieve/store them….

Solution

You should not use var with name IDs. Sugar records use Id to store index values. Use anything other than the ID

public class UserObject extends SugarRecord{
    public UUID uId;
    public String Name;
    public int DefaultSort;
    public String Username;
    public Date LastLogin;

public UserObject()
    {

}

public UserObject(UUID uId, String name, int defaultSort, String username, String password, Date lastLogin)
    {
        this.uId= uId;
        this. Name = name;
        this. DefaultSort = defaultSort;
        this. Username = username;
        this. LastLogin = lastLogin;
    }
}

Starting with v1.4, SugarRecord is no longer required

Related Problems and Solutions