ORMLite: Query empty foreign field
Question
I have two entities: EntityA and EntityB. EntityB has a foreign field of EntityA:
@DatabaseField(foreign=true, columnName=ENT_A_NAME)
private EntityA entityA;
Now I want to query all entries of EntityB where EntityA is null. So I've made the following query:
bDao.queryBuilder().where().isNull(EntityB.Ent_A_NAME).prepare();
If I execute the query I get an empty result set back.
If I execute queryAll()
I see that the entries of EntityB have always an associated Order-Object with all values set to null/0.
How can I execute my query?
Solution
I'm not sure @Toni4780. The following test case works for me. I don't see anything that you are doing wrong.
In the table for EntityB
, ORMLite actually stores the id of the EntityA
so I am wondering if it is null or 0. Have you tried the following?
bDao.queryBuilder().where().eq(EntityB.Ent_A_NAME, 0).prepare();
or both:
bDao.queryBuilder().where().isNull(EntityB.Ent_A_NAME).
or().eq(EntityB.Ent_A_NAME, 0).prepare();
Here's my unit test code that works:
Dao<Order, Integer> orderDao =
DaoManager.createDao(connectionSource, Order.class);
TableUtils.createTable(connectionSource, Order.class);
int numOrders = 10;
for (int orderC = 0; orderC < numOrders; orderC++) {
Order order = new Order();
order.val = orderC;
assertEquals(1, orderDao.create(order));
}
List<Order> results = orderDao.queryBuilder().where()
.isNull(Order.ACCOUNT_FIELD_NAME).query();
assertNotNull(results);
assertEquals(numOrders, results.size());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow