Skip to content

Commit 00ae9a4

Browse files
Unique: add onConflict which accepts a ConflictStrategy.
1 parent 1bcecac commit 00ae9a4

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.objectbox.annotation;
2+
3+
/**
4+
* Used with {@link Unique} to specify the conflict resolution strategy.
5+
*/
6+
public enum ConflictStrategy {
7+
8+
/**
9+
* Default. Throws UniqueViolationException if any property violates a {@link Unique} constraint.
10+
*/
11+
FAIL,
12+
/**
13+
* Ignore the offending object (the existing object is not changed). If there are multiple unique properties in an
14+
* entity, this strategy is evaluated first: if the property conflicts, no other properties will be checked for
15+
* conflicts.
16+
*/
17+
IGNORE,
18+
/**
19+
* The offending object replaces the existing object (deletes it). If there are multiple properties using this
20+
* strategy, a single put can potentially replace (delete) multiple existing objects.
21+
*/
22+
REPLACE,
23+
/**
24+
* The offending object overwrites the existing object while keeping its ID. All relations pointing to the existing
25+
* entity are preserved. This is useful for a "secondary" ID, such as a string "ID". Within an entity, this strategy
26+
* may be used once only (update target would be ambiguous otherwise).
27+
*/
28+
UPDATE
29+
30+
}

objectbox-java-api/src/main/java/io/objectbox/annotation/Unique.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525
* Enforces that the value of a property is unique among all objects in a box before an object can be put.
2626
* <p>
2727
* Trying to put an object with offending values will result in a UniqueViolationException.
28+
* Specify a {@link ConflictStrategy} to change this default behavior.
2829
* <p>
2930
* Unique properties are based on an {@link Index @Index}, so the same restrictions apply.
3031
* It is supported to explicitly add the {@link Index @Index} annotation to configure the index.
3132
*/
3233
@Retention(RetentionPolicy.CLASS)
3334
@Target(ElementType.FIELD)
3435
public @interface Unique {
36+
ConflictStrategy onConflict() default ConflictStrategy.FAIL;
3537
}

0 commit comments

Comments
 (0)