Problem with Inheritance

23 views
Skip to first unread message

javi...@gmail.com

unread,
Oct 4, 2016, 2:44:10 PM10/4/16
to ORMLite 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:44:26 PM10/11/16
to ORMLite 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!
Reply all
Reply to author
Forward
0 new messages