MongoDB の
ObjectId を
対処した
Mongo Driver の Version
3.2.2
です。
<!-- MongoDB --> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>3.2.2</version> </dependency>
エラー内容
ERROR [2017-06-11 17:41:41,495] org.apache.wicket.util.listener.ListenerCollection: Error invoking listener: org.apache.wicket.Application$3@480131f8 ! java.lang.IllegalArgumentException: invalid hexadecimal representation of an ObjectId: [node11qt8o6gp4dtag1uu3a6ksq80g4|||17|||Wicket-MongoDB] ! at org.bson.types.ObjectId.parseHexString(ObjectId.java:523) ! at org.bson.types.ObjectId.<init>(ObjectId.java:237)
原因
ObjectId
に
spring - java.lang.IllegalArgumentException: invalid hexadecimal representation of an ObjectId - Stack Overflow
以下、parseHexString
が
/** * Constructs a new instance from a 24-byte hexadecimal string representation. * * @param hexString the string to convert * @throws IllegalArgumentException if the string is not a valid hex string representation of an ObjectId */ public ObjectId(final String hexString) { this(parseHexString(hexString)); }
parseHexString
から、isValid
メソッドが
/** * Checks if a string could be an {@code ObjectId}. * * @param hexString a potential ObjectId as a String. * @return whether the string could be an object id * @throws IllegalArgumentException if hexString is null */ public static boolean isValid(final String hexString) { if (hexString == null) { throw new IllegalArgumentException(); } int len = hexString.length(); if (len != 24) { return false; } for (int i = 0; i < len; i++) { char c = hexString.charAt(i); if (c >= '0' && c <= '9') { continue; } if (c >= 'a' && c <= 'f') { continue; } if (c >= 'A' && c <= 'F') { continue; } return false; } return true; }
対処方法
キー値に
ユニークインデックスは
無理矢理16進数に
そこまでしなくていいと
補足
ObjectId には、
/** * Constructs a new instance using the given date. * * @param date the date */ public ObjectId(final Date date) { this(dateToTimestampSeconds(date), MACHINE_IDENTIFIER, PROCESS_IDENTIFIER, NEXT_COUNTER.getAndIncrement(), false); }
デフォルトコンストラクタでは、
/** * Create a new object id. */ public ObjectId() { this(new Date()); }
この実装だと、
MACHINE_IDENTIFIER
、PROCESS_IDENTIFIER
、NEXT_COUNTER
がAtomicInteger
などを
IDを
以上です。
コメント