I have an entity class City with a property coordinates I'd like to persist using JPA :
@Entity
public class City {
public static class Point {
protected double x;
protected double y;
// getters and setters
}
protected Point coordinates;
// getter and setter
}
To do that I created an attribute converter to transform Point to PGpoint and vice verca :
@Converter(autoApply = true)
public class PointToPGpointConverter implements AttributeConverter<Point, PGpoint> {
@Override
public PGpoint convertToDatabaseColumn(Point point) {
return new PGpoint(point.getX(), point.getX());
}
@Override
public Point convertToEntityAttribute(PGpoint point) {
return new Point(point.x, point.y);
}
}
But when fetching data, a java.io.StreamCorruptedException is thrown :
invalid stream header: 2834382E
The value corresponds to the hexadecimal representation of "(48.", which is the start of one of the persisted coordinates.
Edit
I believe the serializer is waiting for an Object bytes array but it received a String one. The method throwing the exception is ObjectInputStream.readStreamHeader(). Its OpenJDK implementation (openjdk-jdk11/ObjectInputStream.java#L870) is :
protected void readStreamHeader()
throws IOException, StreamCorruptedException
{
short s0 = bin.readShort();
short s1 = bin.readShort();
if (s0 != STREAM_MAGIC || s1 != STREAM_VERSION) {
throw new StreamCorruptedException(
String.format("invalid stream header: %04X%04X", s0, s1));
}
}
It waits for a STREAM_MAGIC. According to https://stackoverflow.com/a/57631495/6286793
STREAM_MAGIC denotes start of serialzed content.
The problem is when receiving tuples, the PGStream.receiveTupleV3() method gets the bytes array of the string value of the POINT field, not a serialized object.
Is that a bug?