db4o prototype for querybyexample query

153 Views Asked by At

I am using db4o in a Java project. I want to create a prototype to get all the persons that are not married using querybyexample. The Person class has three attributes: name(String), age(int) and married(boolean), so I create the following prototype:

Person prototypePerson = new Person(null, 0, false);

Instead of getting not married persons, I get ALL the persons stored in the DB, both married and unmarried. Could I get unmarried persons using querybyexample or I should use native queries?

1

There are 1 best solutions below

0
Gamlor On

Basically query by example only checks fields which do not have the default value as query parameter. For boolean, 'false' is the default value, so in your example it will be skipped.

You could consider using a enum. Change the field to that enum. Like:

enum MaritalStatus{
    MARRIED,
    SINGLE
}

class Person{

    private String name;
    private MaritalStatus status;

    public Person(String name, MaritalStatus status){
        this.status = status;
        this.name = name;
    }

    // more
}

This makes your code more explicit what it means, and more extensible (like adding Widowed as status) and it should work with query by example. The query would be:

Person prototypePerson = new Person(null, 0, MaritalStatus.SINGLE);