Issue #2291: Can not have unique index on Byte Array

Type: Bug ReoprtVersion: 2.7.4_05Priority: HighStatus: ClosedReplies: 4
#1

I found that it is impossible to insert a record with @Unique byte[] var;

after initial insertion of a single record.

Below is the exception:

 

Nbl3hQfe6LHayqZ7TQKivA==
iWbldTBdz3etGCqHspWw3Q==
Exception in thread "main" [ObjectDB 2.7.4_05] javax.persistence.RollbackException
Failed to commit transaction: Unique constraint (id.co.ecoin.test.objectdb.ObjectDbTest$OdbByteArray[key]) failed: Attempt to reuse an existing value (-121) (error 613)
    at com.objectdb.jpa.EMImpl.commit(EMImpl.java:289)
    at id.co.ecoin.test.objectdb.ObjectDbTest$OdbByteArray.newInstance(ObjectDbTest.java:87)
    at id.co.ecoin.test.objectdb.ObjectDbTest.objectDbBasic2(ObjectDbTest.java:52)
    at id.co.ecoin.test.objectdb.ObjectDbTest.main(ObjectDbTest.java:19)
Caused by: javax.persistence.PersistenceException: com.objectdb.o.UserException: Unique constraint (id.co.ecoin.test.objectdb.ObjectDbTest$OdbByteArray[key]) failed: Attempt to reuse an existing value (-121)
    at com.objectdb.o._PersistenceException.a(_PersistenceException.java:47)
    at com.objectdb.o.JPE.d(JPE.java:147)
    at com.objectdb.o.JPE.d(JPE.java:80)
    ... 6 more
Caused by: com.objectdb.o.UserException: Unique constraint (id.co.ecoin.test.objectdb.ObjectDbTest$OdbByteArray[key]) failed: Attempt to reuse an existing value (-121)
    at com.objectdb.o.MSG.a(MSG.java:64)
    at com.objectdb.o.SPW.ap(SPW.java:323)
    at com.objectdb.o.SPW.ar(SPW.java:267)
    at com.objectdb.o.SPW.ac(SPW.java:115)
    at com.objectdb.o.PGW.ak(PGW.java:221)
    at com.objectdb.o.UPT.s(UPT.java:135)
    at com.objectdb.o.URT.g(URT.java:173)
    at com.objectdb.o.TSK.b(TSK.java:146)
    at com.objectdb.o.TSK.c(TSK.java:96)
    at com.objectdb.o.TSM.a(TSM.java:88)
    at com.objectdb.o.UTT.r(UTT.java:377)
    at com.objectdb.o.UTT.g(UTT.java:225)
    at com.objectdb.o.TSK.b(TSK.java:146)
    at com.objectdb.o.TSK.c(TSK.java:96)
    at com.objectdb.o.TSM.a(TSM.java:88)
    at com.objectdb.o.MST.Zy(MST.java:1421)
    at com.objectdb.o.WRA.Zy(WRA.java:398)
    at com.objectdb.o.WSM.Zy(WSM.java:186)
    at com.objectdb.o.OBM.aP(OBM.java:955)
    at com.objectdb.o.OBM.aS(OBM.java:855)
    at com.objectdb.o.OBM.aG(OBM.java:755)
    at com.objectdb.jpa.EMImpl.commit(EMImpl.java:286)
    ... 3 more
#2

Unique is implemented by ObjectDB by an index that does not allow duplicate keys. An index on a collection or array (including byte[]) treats every element in the collection or the array as a separate entry.

Therefore, if a field of collection or array type is declared as unique an exception is thrown if any element is repeating more that once even if there are no 2 arrays or collections that are identical. For example if obj1 has [0, 1, 2] and obj2 has [2, 3, 4] in the unique byte[] field, an exception is thrown because of the duplicate element '2', although the 2 byte[] arrays are different.

ObjectDB Support
#3

Note also that an index on a collection or array will not be useful in queries that compare the entire array or collection. It will only improve performance in queries that iterate over elements in the collection or array, e.g. using a JOIN query. This is because every element is indexed as a separate index entry.

ObjectDB Support
#4

Ok. Thank you. For the explanation.

BTW, is String implemented in a same way to byte[], that is {'a', 'b', 'c'} and {'c', 'd', 'e'} will throw Unique exception because of duplicate element 'a'?

Thanks.

#5

Strings are not considered as collections or arrays but as values.

ObjectDB Support

Reply