This is an automated email from the ASF dual-hosted git repository.

mridulpathak pushed a commit to branch release24.09
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


The following commit(s) were added to refs/heads/release24.09 by this push:
     new 29827c4109 Store and read blob fields as bytes on binary columns
29827c4109 is described below

commit 29827c4109c653edc0ad592dc78dd71aa364e50a
Author: Mridul Pathak <[email protected]>
AuthorDate: Tue Oct 27 12:09:32 2026 +0530

    Store and read blob fields as bytes on binary columns
    
    On PostgreSQL the blob field type is a BYTEA column, but the PostgreSQL 
JDBC driver maps setBlob and getBlob to large objects, so storing a Blob failed 
with "column is of type bytea but expression is of type bigint" and reading one 
failed with "Bad value for type long". When the column's SQL type is binary, 
the blob value handler now binds the value with setBytes and reads it back with 
getBytes wrapped in a SerialBlob. Other databases keep using setBlob and 
getBlob.
---
 .../apache/ofbiz/entity/jdbc/JdbcValueHandler.java   | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git 
a/framework/entity/src/main/java/org/apache/ofbiz/entity/jdbc/JdbcValueHandler.java
 
b/framework/entity/src/main/java/org/apache/ofbiz/entity/jdbc/JdbcValueHandler.java
index 07f582cd6d..295b1fc6c2 100644
--- 
a/framework/entity/src/main/java/org/apache/ofbiz/entity/jdbc/JdbcValueHandler.java
+++ 
b/framework/entity/src/main/java/org/apache/ofbiz/entity/jdbc/JdbcValueHandler.java
@@ -339,12 +339,30 @@ public abstract class JdbcValueHandler<T> {
         public Class<Object> getJavaClass() {
             return Object.class;
         }
+        /**
+         * Returns true when the column is a binary type (like PostgreSQL 
BYTEA) rather than a BLOB.
+         * Some drivers, like PostgreSQL's, map the JDBC Blob methods to large 
objects, which cannot
+         * be used with such a column, so the value is passed as bytes instead.
+         */
+        private boolean isBinaryColumn() {
+            int sqlType = getSqlType();
+            return sqlType == Types.BINARY || sqlType == Types.VARBINARY || 
sqlType == Types.LONGVARBINARY;
+        }
         @Override
         protected void castAndSetValue(PreparedStatement ps, int 
parameterIndex, Object obj) throws SQLException {
-            ps.setBlob(parameterIndex, (Blob) obj);
+            Blob blob = (Blob) obj;
+            if (isBinaryColumn()) {
+                ps.setBytes(parameterIndex, blob.getBytes(1, (int) 
blob.length()));
+            } else {
+                ps.setBlob(parameterIndex, blob);
+            }
         }
         @Override
         public Object getValue(ResultSet rs, int columnIndex) throws 
SQLException {
+            if (isBinaryColumn()) {
+                byte[] bytes = rs.getBytes(columnIndex);
+                return bytes != null ? new SerialBlob(bytes) : null;
+            }
             Blob fieldBlob = rs.getBlob(columnIndex);
             if (fieldBlob != null) {
                 return new SerialBlob(fieldBlob);

Reply via email to