With the release of ObjectBox 2.0 we are hitting a new milestone on our journey to make embedded databases simpler to use and faster. Two of the highlights we already announced in our 2.0 beta post: links (aka joins) and relation completeness. The final release brings some refinements to those and brings additional features like unique constraints and hash-based indexing.
1 2 | @Unique String serverKey; |
Putting the new @Unique annotation on a property enforces its value to be unique. If you try to put an object with has the same property value of an already existing entity, the put operation will be rejected and an exception will be thrown. In order to check efficiently for uniqueness, @Unique implicitly creates an index for the property.
For properties of type String, ObjectBox now defaults to hash-based indexing. Especially if you are indexing long strings this reduces storage space significantly over value-based indexing. However you can still opt to value based indexing. While hash-based indexes work for the very common “equal” condition, “starts with” conditions can only use value-based indexes. You can configure this like this:
1 2 3 | @Unique @Index(type = VALUE) String serverKey; |
With the new link feature (see the 2.0 beta post), ObjectBox 2.0 can extend queries over multiple entity types. To make referencing properties in QueryBuilder safer, we added the type info API. Thus, the referenced property must match its entity type or you will get an compiler error early on:
1 2 3 4 5 6 | import com.example.Person_.name; // Generated meta info ... QueryBuilder<Person> builder = personBox.query(); builder.equal(name, "Amy") // imported .link(Person_.address) .equal(name, "Main Street"); // <-- Compiler error |
In this example, name is a property of the Person entity, and thus it would be an error to use it for Address entity, to which the link is made.
To highlight a few query improvements in 2.0: “in”, “greater”, and “less” conditions were added for Strings, and you can now count properties for non-null and unique occurrences. For a full list of 2.0 improvements, please check the changelog.
While 2.0 is taking a big step, it’s just one of many and ObjectBox still has much further to go. Let us know what matters most to you so we can prioritize. The best way for us to track your feature preference is giving +1 reactions (thumbs up emoji) on our issue tracker. The next leap on our road map is already set: data synchronization and you can already sign up for early access.
PS.: With 2.0 out, we also launched at Product Hunt.
Cool. Gotta try the new stuff ?
Unique constraints out just in time for business app I’m developing for the company.
Awesome!!!
So great! You guys awesome!
How does Unique work when putting a collection?
Thanks