Is there something similar to this: @Insert(onConflict = OnConflictStrategy.REPLACE) as found in Room database that you can do in Objectbox. If not, how can you avoid saving duplicate entries in an objectbox entity? Thanks.
3 Answers
in object box DataModel class, make the item anotation
@unique Public String userID;
private Box<User> userBox;
userBox = ObjectBox.get().boxFor(User.class);
//your inserted object
if (!userBox.getAll().contains(insertedObject)){
leadBoxForLeadOpions.put(l);
}

- 668
- 11
- 22
Replacing objects should be simple enough as setting the @id(assignable=true)
annotation to the wanted id field. I have tested it in my android app via:
@Test
fun testAssignableIdsGetReplaced() {
val myUser1 = User()
myUser1.id = 1
myUser1.nickname = "Original"
var currentList = boxStore!!.boxFor(User::class.java).all
assert(currentList.size == 0)
boxStore!!.boxFor(User::class.java).put(myUser1)
currentList = boxStore!!.boxFor(User::class.java).all
assert(currentList.size == 1)
assert(currentList.get(0).id == 1L)
val newUser1 = User()
newUser1.id = 1
newUser1.nickname = "New"
boxStore!!.boxFor(User::class.java).put(newUser1)
currentList = boxStore!!.boxFor(User::class.java).all
assert(currentList.size == 1)
assert(currentList.get(0).id == 1L)
assert(currentList.get(0).nickname == newUser1.nickname)
}
@Entity
class User() {
@Id(assignable = true) var id: Long = 0
lateinitvar nickname: String
}
But bare in mind that using ids assignable annotation may produce sideeffects:
https://docs.objectbox.io/relations#updating-relations
https://docs.objectbox.io/advanced/object-ids#self-assigned-object-ids

- 408
- 2
- 9
- 16
Just use the @Unique()
annotation in the field you wanna make unique, like this:
@Entity()
class FooEntity {
@Id()
int id;//some id
@Unique()
int fooField;//unique field
}
By objectBox docs:
/// Enforces that the value of a property is unique among all objects in a box
/// before an object can be put.
///
/// Trying to put an object with offending values will result in a
/// [UniqueViolationException] (see [ConflictStrategy.fail]).
/// Set [onConflict] to change this strategy.
///
/// Note: Unique properties are based on an [Index], so the same restrictions apply.
/// It is supported to explicitly add the [Index] annotation to configure the
/// index.
class Unique {
/// The strategy to use when a conflict is detected when an object is put.
final ConflictStrategy onConflict;
/// Create a Unique annotation.
const Unique({this.onConflict = ConflictStrategy.fail});
}
/// Used with [Unique] to specify the conflict resolution strategy.
enum ConflictStrategy {
/// Throws [UniqueViolationException] if any property violates a [Unique] constraint.
fail,
/// Any conflicting objects are deleted before the object is inserted.
replace,
}
By default you will have the fail behavior, it mean, if the value in fooField
is already storaged, an error will be throw by objetbox. In the other hand, if you put the onConflict
property in the Unique
annotation to ConflictStrategy.replace
, the object will be replaced.
The final approuch will be like this:
@Entity()
class FooEntity {
@Id()
int id;//some id
@Unique(onConflict: ConflictStrategy.replace)
int fooField;
}
If you wanna make it on the id of the class, just use the Unique
and Id
on the same field. Like this:
@Entity()
class FooEntity {
@Id()
@Unique(onConflict: ConflictStrategy.replace)
int id;//some id
int fooField;
}
Hope this solve the problem. It did it for my.

- 189
- 1
- 10