Skip to content

Commit 9ca1f17

Browse files
committed
add @unique
1 parent 8172529 commit 9ca1f17

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 ObjectBox Ltd. All rights reserved.
2+
* Copyright 2017-2018 ObjectBox Ltd. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,8 @@
2323

2424
/**
2525
* Specifies that the property should be indexed, which is highly recommended if you do queries using this property.
26+
*
27+
* To fine tune indexing you can specify {@link IndexType} and/or use {@link #maxValueLength()} if necessary.
2628
*/
2729
@Retention(RetentionPolicy.CLASS)
2830
@Target(ElementType.FIELD)
@@ -32,8 +34,4 @@
3234
/** Only allowed for {@link IndexType#VALUE} and types String and byte[]. */
3335
int maxValueLength() default 0;
3436

35-
// /**
36-
// * Whether the unique constraint should be created with base on this index
37-
// */
38-
// boolean unique() default false;
3937
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2017 ObjectBox Ltd. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.objectbox.annotation;
18+
19+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.RetentionPolicy;
22+
import java.lang.annotation.Target;
23+
24+
/**
25+
* Marks values of a property to be unique.
26+
* The property will be indexed behind the scenes, just like using @{@link Index}.
27+
* Thus you do not need to put an extra @{@link Index} on the property, unless you want to configure the index with
28+
* additional parameters.
29+
*
30+
* Trying to put object with offending values will result in a UniqueViolationException.
31+
*/
32+
@Retention(RetentionPolicy.CLASS)
33+
@Target(ElementType.FIELD)
34+
public @interface Unique {
35+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2017-2018 ObjectBox Ltd. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.objectbox.exception;
18+
19+
/** Thrown when a @{@link io.objectbox.annotation.Unique} constraint would be violated during a put operation. */
20+
public class UniqueViolationException extends DbException {
21+
public UniqueViolationException(String message) {
22+
super(message);
23+
}
24+
}

0 commit comments

Comments
 (0)