Relations Understanding-Problem

43 views
Skip to first unread message

Emanuel

unread,
Jul 31, 2012, 6:28:11 AM7/31/12
to gree...@googlegroups.com
Hi,

i'm using greenDao for the first time. I want to save some data and experienced problems with relations. Here is my szenario:

In my app I want to save some favorites I've chosen before. A favorite is an entity with some StringProperties. Besides that I have other entities. I called them "prices" and "services". A services and a prices -entity has some StringProperties on their own. 

Because a price and a service belongs to a favorite I want to set them in relation to each other. If the entities prices and services are some kind of properties of favorite, I could persist them via the favorite object like this:

...
Favorite fav = new Favorite();
fav.setSomeStringProperty("...");
fav.setPrice(prices);
fav.setService(services);

Can you tell me if this is possible?

What I already done so far is that favorite has the fields price and service but all i can do is read and not write or set the data.
I think i'm missing something or didn't totally get how to do it. Here is some code from my DaoGenerator:

Entity favourite = schema.addEntity("Favourite");
favourite.addIdProperty();
favourite.addStringProperty("addressName");
favourite.addStringProperty("addressStreet");

Entity prices = schema.addEntity("Prices");
prices.setTableName("PRICES");
prices.addIdProperty();
prices.addStringProperty("pricesResultValue");
prices.addStringProperty("pricesResultKey");
favId = prices.addLongProperty("favId").notNull().getProperty();
prices.addToOne(favourite, favId);
ToMany pricesToFavs = favourite.addToMany(prices, favId);
pricesToFavs.setName("prices");

Entity services = schema.addEntity("Services");
services.setTableName("SERVICES");
services.addIdProperty();
services.addStringProperty("servicesResultIcon");
services.addStringProperty("servicesResultValue");
favId = services.addLongProperty("favId").notNull().getProperty();
services.addToOne(favourite, favId);
ToMany servicesToFavs = favourite.addToMany(services, favId);
servicesToFavs.setName("services");

Many thanks in advance!!
Emanuel

Emanuel

unread,
Aug 1, 2012, 3:24:24 AM8/1/12
to gree...@googlegroups.com
Fixed.
1) I removed the ToOne relation of the depending entities Prices and Services. 
2) I used the wrong object to persist the entities Prices and Services. Due to the documentation I thought they would be persisted to the general daoSession as described here

List orders = customer.getOrders();
newOrder.setCustomerId(customer.getId());
daoSession.insert(newOrder);
orders.add(newOrder);

What needs to be done is to persist an entity to its own dao. What I do is something like this

Services services = new Services();
services.setSomeProperty;
services.setFavoriteId(fav.getId());
this.daoSession.getServiceDao().insert(services);    <-- here i get the serviceDao to do the insert

Markus Junginger

unread,
Aug 2, 2012, 8:54:10 AM8/2/12
to gree...@googlegroups.com
Fixed.
Glad you figured it out.

Let me add a comment to your point 2): it should not make any difference if you persist an entity using DaoDession or a concrete DAO. The session just delegates to the DAO.

Markus
Reply all
Reply to author
Forward
0 new messages