Ollie icon indicating copy to clipboard operation
Ollie copied to clipboard

Items not saving in database

Open heatherSnepenger opened this issue 10 years ago • 5 comments

I'm trying to save a model to the database, and it's not saving for some reason. I have another model that saves fine. I can't seem to nail down the issue.

The object I'm trying to save is call "Message". Here's what it looks like:

public class Message extends Model {

    @Column("message_slug")
    public String message_slug;

    @Column("message_context")
    public String message_context;

    @Column("message_description")
    public String message_description;

    public Message() {
    }

    public String getMessage_slug() {
        return message_slug;
    }

    public String getMessage_description() {
        return message_description;
    }
}

When I try to save the messages in this way, they don't get saved to the database:

                ArrayList<Message> messages = gb.create().fromJson(o.getJSONArray("messages").toString(), listType);

                // bulk insert the messages
                Ollie.getDatabase().beginTransaction();
                try {
                    for (Message message : messages) {
                        message.save();
                    }
                    Ollie.getDatabase().setTransactionSuccessful();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                finally {
                    Ollie.getDatabase().endTransaction();
                } 

If I create the Message as save as follows, it does get added to the database:

    Message message = new Message();
    message.context = "context";
    message.message_description = "description";
    message.message_slug = "message_slug";
    message.save();

I'm not getting any errors or anything to indicate why the messages wouldn't be saving. Do you know way the messages wouldn't be getting added?

heatherSnepenger avatar Mar 19 '15 17:03 heatherSnepenger

Ok I figured out why the objects aren't saving, but I don't know how to fix it. When the id of the object is set, it does not get saved to the database. I need a way to save the id of object coming from the API. Does Ollie support that?

heatherSnepenger avatar Mar 19 '15 20:03 heatherSnepenger

I can insert into the database, just not save. This works:

Insert.into(Message.class).columns("_id", "message_slug", "message_context").values(120, "tester", "blah").execute();

heatherSnepenger avatar Mar 19 '15 20:03 heatherSnepenger

I got the following code block working:

    Message message = Select.from(Message.class).where(Model._ID + "= '150'").fetchSingle();
    if (message == null) {
        message = new Message();
        message.id = new Long(150);
        Insert.into(message.class).columns(Model._ID).values(message.id).execute();
        message.message_description = "test123";
        message.message_slug = "mr test";
        message.save();
    } else {
        message.message_description = "testing again";
        message.save();
    }

Note that I have to manually insert the object the first time it is added. After that, I can update it fine. I have to check first to see if the model exists in the database. Isn't this something that the save() method should handle on it's own?

heatherSnepenger avatar Mar 19 '15 21:03 heatherSnepenger

Here was my final solution. I created a class that extends from Model, and all of my models now extend SLModel. I call the "saveModel" method, and things are working:

public class SLModel extends Model {

public void saveModel() {
    Model dbMessage = Select.from(getClass()).where(Model._ID + "=" + this.id).fetchSingle();
    // message isn't in db yet
    if (dbMessage == null) {
        Insert.into(getClass()).columns(Model._ID).values(this.id).execute();
    }
    // Save the model with all the attributes
    save();
}
}

heatherSnepenger avatar Mar 20 '15 13:03 heatherSnepenger

I think you maybe use 0.3.2-SNAPSHOT

liuchenx avatar Mar 27 '15 15:03 liuchenx