Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Bug Fixes:

CAY-3071 DataNodeSQLAction could lose update count info
CAY-3073 AI plugin / MCP db import schema description inconsistencies
CAY-3080 ObjectId.toString() prints binary key values as object identity

----------------------------------
Release: 5.0-RC1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ public String toString() {
String[] keys = objectIdKeys.keySet().toArray(new String[0]);
Arrays.sort(keys);
for (String key : keys) {
buffer.append(", ").append(key).append("=").append(objectIdKeys.get(key));
buffer.append(", ").append(key).append("=");
ObjectIdSingle.appendIdValue(buffer, objectIdKeys.get(key));
}
return buffer.append(">").toString();
}
Expand Down
20 changes: 19 additions & 1 deletion cayenne/src/main/java/org/apache/cayenne/ObjectIdSingle.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.apache.cayenne;

import org.apache.cayenne.util.IDUtil;
import org.apache.cayenne.util.SingleEntryMap;

import java.util.Arrays;
Expand Down Expand Up @@ -87,7 +88,24 @@ public boolean isReplacementIdAttached() {

@Override
public String toString() {
return "<ObjectId:" + entityName + ", " + keyName + "=" + value + ">";
StringBuilder buffer = new StringBuilder().append("<ObjectId:").append(entityName).append(", ")
.append(keyName).append("=");
appendIdValue(buffer, value);
return buffer.append(">").toString();
}

/**
* Appends an id value for {@link #toString()}, printing a byte[] (e.g. a binary PK) as a hex string, so that equal
* ids produce the same string.
*/
static void appendIdValue(StringBuilder buffer, Object value) {
if (value instanceof byte[] bytes) {
for (byte b : bytes) {
IDUtil.appendFormattedByte(buffer, b);
}
} else {
buffer.append(value);
}
}

String getKeyName() {
Expand Down
27 changes: 27 additions & 0 deletions cayenne/src/test/java/org/apache/cayenne/ObjectIdTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,31 @@ public void toStringMethod() {
assertEquals(i1, i2);
assertEquals(i1.toString(), i2.toString());
}

@Test
public void toStringBinaryKey() {
ObjectId i1 = ObjectId.of("E", "ID", new byte[]{1, 2, 10, -1});
ObjectId i2 = ObjectId.of("E", "ID", new byte[]{1, 2, 10, -1});

assertEquals("<ObjectId:E, ID=01020AFF>", i1.toString());
assertEquals(i1, i2);
assertEquals(i1.toString(), i2.toString());
}

@Test
public void toStringCompoundBinaryKey() {
Map<String, Object> m1 = new HashMap<>();
m1.put("a", new byte[]{1, 2, 10, -1});
m1.put("b", 5);
ObjectId i1 = ObjectId.of("E", m1);

Map<String, Object> m2 = new HashMap<>();
m2.put("b", 5);
m2.put("a", new byte[]{1, 2, 10, -1});
ObjectId i2 = ObjectId.of("E", m2);

assertEquals("<ObjectId:E, a=01020AFF, b=5>", i1.toString());
assertEquals(i1, i2);
assertEquals(i1.toString(), i2.toString());
}
}
Loading