0

I have already tried use polymorphic in greenDao, but not successful. How to use polymorphic in greenDao ?

for example:

I have a table "my_favorites", in that i can store id foreign key of Events, Pictures, Music ... ?

I hope for your help!

Community
  • 1
  • 1
Luan D
  • 1,320
  • 1
  • 13
  • 26

2 Answers2

0

I'm afraid this is not supported by greendao at the moment.

You will have to store the keys to a normal 'LongProperty' without any 'toOne' or 'toMany' and in a second column you will have to save a discriminator value (i.e. the referenced classname).

Then you will have to build the queries yourself (i.e. in the 'KEEP-SECTION' of your 'my_favorites'.

public Object getDetail() throws ClassNotFoundException {
    if (daoSession== null) {
        throw new DaoException("Entity is detached from DAO context");
    }
    return daoSession.load(Class.forName(getDiscriminator()), getMyDetailId());
}
AlexS
  • 5,295
  • 3
  • 38
  • 54
0

you can use ToMany relation in your DB. Look at this tutorial

Entity customer = schema.addEntity("Customer");
customer.addIdProperty();
customer.addStringProperty("name").notNull();

Entity order = schema.addEntity("Order");
order.setTableName("ORDERS"); // "ORDER" is a reserved keyword
order.addIdProperty();
Property orderDate = order.addDateProperty("date").getProperty();
Property customerId = order.addLongProperty("customerId").notNull().getProperty();
order.addToOne(customer, customerId);

ToMany customerToOrders = customer.addToMany(order, customerId);
customerToOrders.setName("orders");
customerToOrders.orderAsc(orderDate);
SerCna
  • 278
  • 2
  • 12
  • You should add the most relevant part of your link, in case the target site is unreachable or goes permanently offline. Read [How do I write a good answer](http://stackoverflow.com/help/how-to-answer) for more details. – Luís Cruz Aug 23 '14 at 12:13
  • It is online now and whenever i check it. please check again. – SerCna Aug 24 '14 at 06:06
  • I understand that its online. However, 6 months or 3 years from now, it might not be. From the [How do I write a good answer](http://stackoverflow.com/help/how-to-answer): _Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline._ This is only a suggestion in order to further improve your question. – Luís Cruz Aug 24 '14 at 11:47
  • Thanks Milz. I will do it. – SerCna Aug 25 '14 at 04:29