0

I have two entities: "Orders" and "WorkPosition". The Workpositions are related through the id of the orders, that means i have an toMany-Relationship here. One order have "n" Workpositions and one Workpostion has always an order_id. So there is a function: order.getWorkPositions().

What i am doing right now: Rightnow i am querying all Orders and then i am iterating through the Orders. If an order has workpositions that have workposition.type="1".

The problem is that this is very slow, because i have at least 1000 orders and even more workpositions.

Question: I saw that the "join" function is not working, but on the greenDao Homepage i saw the following Query:

Query query = userDao.queryBuilder().where(
new StringCondition("_ID IN " +
"(SELECT USER_ID FROM USER_MESSAGE WHERE READ_FLAG = 0)").build();

How can i use it to speedup my query? Can i have orders as a returntype from the query? Or can i have only a list of ids?

As a query i want something like this:

Query<Orders> query = getDAOSession().getOrdersDao().queryBuilder()
.where(new StringCondition("?_ID IN? "+ 
"(SELECT * FROM Orders AS O JOIN WorkPosition AS W WHERE w.type=1)")).build();
List<Orders> orders = query.list();

I hope you can understand my question, if not tell me and i will try to improve it!:)

oli
  • 569
  • 6
  • 16

2 Answers2

1

In my experience I usually use the raw query for join statements. Only Ormlite supports partially join, but it is very simple and not usable for me in all cases. My proposition - use raw query and cursors

  • actually for me this codelines work perfect if you only want informations of one entity, but you want to filter with a join. its a very simple solution – oli Dec 04 '14 at 10:24
0

The "join"-function is only not supported by the greendao-api. This means you cannot build joins using the equal named function of Property.

But you can build every WhereCondition you want as long as it is supported by SQLite. This means your query will work, if you join correctly. (I noticed that you didn't specify "ON"-statement).

AlexS
  • 5,295
  • 3
  • 38
  • 54
  • thank you. actual its working without an "on"-statement. my problems was that i tried to add a between two dates query. I found out that greenDao wants not the Date itself. It wants date.getTime() – oli Apr 29 '14 at 10:59
  • @oli Nevertheless I'd recommend using the "on"-statement together with the columnnames defined in "userdao.Properties". This way you keep control over your join and changing your schema is unlikely to break your join. On top, if your schema changes and some columns you are relying on in your statement will produce a compiletime error instead of runtime error. – AlexS Apr 30 '14 at 09:34