Problem with Inheritance

21 views
Skip to first unread message

javi...@gmail.com

unread,
Oct 11, 2016, 2:48:23 PM10/11/16
to ORMLite Android Users
Hi!

I have this scenario:

@DatabaseTable(tableName="Foo")
public class Foo {
@DatabaseField(generatedId = true)
protected int id;
@ForeignCollectionField(eager=true)
private Collection<Parent> parentList;
}

public abstract class Parent {
@DatabaseField(generatedId = true)
protected int id;
@DatabaseField
protected String description; 
@DatabaseField(foreign = true)
protected Foo foo;
}

@DatabaseTable(tableName="ChildA")
public class ChildA extends Parent {
@DatabaseField
private String somethingA;
}

@DatabaseTable(tableName="ChildB")
public class ChildB extends Parent {
@DatabaseField
private int somethingB;
}

and to save this i do:

fooDao.createOrUpdate(foo);

for (Parent parent : foo.getParentList()) {

parent.setFoo(foo);
if (parent instanceof ChildA){
childA_DAO.createOrUpdate((ChildA) parent);
}
else if (parent instanceof ChildB){
childB_DAO.createOrUpdate((ChildB) parent);
}
}

and that works fine, I can see everything saved on the SQLite database (through a SQLite Viewer).
But when I do:

fooDao.queryForAll();

i got:
(1) no such table: parent
java.sql.SQLException: Problems executing Android query: SELECT * FROM `parent` WHERE `foo_id` = ? 

I think I have to do something like the save code, but I don't know where and how?
Can anyone help me?
Thanks

P/D: Sorry for my english, I speak spanish. I understand english but is difficult for me to write in english.

javi...@gmail.com

unread,
Oct 11, 2016, 2:48:49 PM10/11/16
to ORMLite Android Users
UPDATE:

I tried changing the database schema to save all the subclases in the same table like this:

public abstract class Parent {
 @DatabaseField(generatedId = true)
 protected int id;
 ...

@DatabaseTable(tableName="parent")
public class ChildA extends Parent {...

@DatabaseTable(tableName="parent")
public class ChildB extends Parent {...


but now, I'm getting this exception:

java.sql.SQLException: Could not create object for class ar.com.sample.Parent

it's trying to create an object of the abstract class!
I don't know how to solve this problem!

Gary Spreder

unread,
Dec 21, 2016, 4:01:36 PM12/21/16
to ORMLite Android Users
Hello,

I am sorry to say that what you are trying to achieve is simply not supported by ORMLite.

In your first example, you're trying to use a ParentDAO to conditionally save to either table "ChildA" or table "ChildB".
In your second example, you're trying to save the children in the same table.

Any given DAO handles queries against a single table, and any given table has a particular Class file associated with it.
The idea of running queries which span multiple tables based on some abstract notion of inheritance is beyond the scope of ORMLite.
I do not have a link on hand, but if memory serves, Gray Watson said that this falls outside of what he considers "Lite".

Sorry,
Gary
Reply all
Reply to author
Forward
0 new messages