ahmetb / orman

lightweight and minimalist ORM for Java/Android. works with SQLite & MySQL. (not actively maintained)
Other
249 stars 47 forks source link

Model.update() problem #34

Closed almozavr closed 13 years ago

almozavr commented 13 years ago

Hi.

I'm forcing with strange update() problem. I have a quite complicated entity which has ManyToMany (or OneToMany) and a problem appears when I try to update entity which is already in DB. So even if I fetch entity from DB and then update the orman fails to do it giving following exception:

09-19 09:26:19.830: ERROR/AndroidRuntime(318): Caused by: org.orman.dbms.exception.QueryExecutionException:
         Query execution error: SQLiteAndroid error:android.database.sqlite.SQLiteException:
         near ".": syntax error: UPDATE stream SET message=1, messages_using_it=(com.sample.project.model.Message@f879683e),
         title='New discussion', users=(com.sample.project.model.User@f6fdfd03)  WHERE row_id = 1;
09-19 09:26:19.830: ERROR/AndroidRuntime(318):     at org.orman.dbms.sqliteandroid.QueryExecutionContainerImpl.throwError(QueryExecutionContainerImpl.java:30)
09-19 09:26:19.830: ERROR/AndroidRuntime(318):     at org.orman.dbms.sqliteandroid.QueryExecutionContainerImpl.executeOnly(QueryExecutionContainerImpl.java:41)
09-19 09:26:19.830: ERROR/AndroidRuntime(318):     at org.orman.mapper.Model.update(Model.java:164)
ahmetb commented 13 years ago

What is type of messages_using_it field in Java? Can you share a few related fields of Stream class with us please so that we can locate the bug much easier.

almozavr commented 13 years ago

Certainly.

Shortly, every Stream may have many Messages and some Message could belong to only one Stream.

Stream

@Entity
public class Stream extends BasicDbObject<Stream> implements Parcelable {

@PrimaryKey(autoIncrement = true)
private int rowId;
@Index(unique = true)
private long id;
private Message message;
@ManyToOne
private User owner;
private String time;
private String title;
private String privacy;

@OneToMany(toType = Message.class, onField = "stream", load = LoadingPolicy.LAZY)
public EntityList<Stream, Message> messagesUsingIt =
                new EntityList(Stream.class, Message.class, this);

@ManyToMany(toType = User.class, load = LoadingPolicy.LAZY)
public EntityList<Stream, User> users =
                new EntityList(Stream.class, User.class, this);
...
}

Message

@Entity
public class Message extends BasicDbObject<Message> implements Parcelable {

@PrimaryKey(autoIncrement = true)
private int rowId;
@Index(unique = false)
private long id;

private String text;
private String time;
private long parentMessageId;
private int rating;
@ManyToOne
private Stream stream;
@ManyToOne
private User user;

...
}

BasicDbObject is just a simple abstract helper class

public abstract class BasicDbObject<E> extends Model<E> {

public static String TABLE_COLUMN_ID = "id";

public abstract void copy(E copySource);
public abstract long getId();

}

almozavr commented 13 years ago

Also, I must admit that everything works for Stream.insert(). I tested it on 500+ records and it's really fine.

But if I let's say

Stream s = (Stream) fetchSingle(...)
s.update();

It fails with provided error.

ahmetb commented 13 years ago

Did this solve your problem? Can you please rebuild?

almozavr commented 13 years ago

Thanks, great! It seems to work without any error exceptions!

ahmetb commented 13 years ago

Glad it is solved.