Skip to content

Commit

Permalink
Change JsonBinarySqlTypeDescriptor to bind the JSON object as String #…
Browse files Browse the repository at this point in the history
  • Loading branch information
vladmihalcea committed Apr 10, 2022
1 parent e976235 commit a2f92c0
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.vladmihalcea.hibernate.type.json.internal;

import com.fasterxml.jackson.databind.JsonNode;
import org.hibernate.type.descriptor.ValueBinder;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.sql.BasicBinder;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.SQLException;

Expand All @@ -22,7 +20,7 @@ public <X> ValueBinder<X> getBinder(final JavaTypeDescriptor<X> javaTypeDescript
return new BasicBinder<X>(javaTypeDescriptor, this) {
@Override
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
st.setObject(index, javaTypeDescriptor.unwrap(value, JsonNode.class, options), getSqlType());
st.setObject(index, javaTypeDescriptor.unwrap(value, String.class, options), getSqlType());
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.vladmihalcea.hibernate.type.json.internal;

import com.fasterxml.jackson.databind.JsonNode;
import org.hibernate.type.descriptor.ValueBinder;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.sql.BasicBinder;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.SQLException;

Expand All @@ -22,7 +20,7 @@ public <X> ValueBinder<X> getBinder(final JavaTypeDescriptor<X> javaTypeDescript
return new BasicBinder<X>(javaTypeDescriptor, this) {
@Override
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
st.setObject(index, javaTypeDescriptor.unwrap(value, JsonNode.class, options), getSqlType());
st.setObject(index, javaTypeDescriptor.unwrap(value, String.class, options), getSqlType());
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.vladmihalcea.hibernate.type.json.internal;

import com.fasterxml.jackson.databind.JsonNode;
import org.hibernate.type.descriptor.ValueBinder;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
Expand All @@ -22,13 +21,13 @@ public <X> ValueBinder<X> getBinder(final JavaTypeDescriptor<X> javaTypeDescript
return new BasicBinder<X>(javaTypeDescriptor, this) {
@Override
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
st.setObject(index, javaTypeDescriptor.unwrap(value, JsonNode.class, options), getSqlType());
st.setObject(index, javaTypeDescriptor.unwrap(value, String.class, options), getSqlType());
}

@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
st.setObject(name, javaTypeDescriptor.unwrap(value, JsonNode.class, options), getSqlType());
st.setObject(name, javaTypeDescriptor.unwrap(value, String.class, options), getSqlType());
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.vladmihalcea.hibernate.type.json.internal;

import com.fasterxml.jackson.databind.JsonNode;
import org.hibernate.type.descriptor.ValueBinder;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
Expand All @@ -22,13 +21,13 @@ public <X> ValueBinder<X> getBinder(final JavaTypeDescriptor<X> javaTypeDescript
return new BasicBinder<X>(javaTypeDescriptor, this) {
@Override
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
st.setObject(index, javaTypeDescriptor.unwrap(value, JsonNode.class, options), getSqlType());
st.setObject(index, javaTypeDescriptor.unwrap(value, String.class, options), getSqlType());
}

@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
st.setObject(name, javaTypeDescriptor.unwrap(value, JsonNode.class, options), getSqlType());
st.setObject(name, javaTypeDescriptor.unwrap(value, String.class, options), getSqlType());
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.vladmihalcea.hibernate.type.json;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonNode;
import com.vladmihalcea.hibernate.util.AbstractPostgreSQLIntegrationTest;
import com.vladmihalcea.hibernate.util.ExceptionUtil;
Expand Down Expand Up @@ -152,8 +151,8 @@ public void testInvalidJson() {
});
fail("An invalid JSON should throw an exception!");
} catch (Exception e) {
JsonParseException rootCause = ExceptionUtil.rootCause(e);
assertTrue(rootCause.getMessage().contains("Unexpected character (':'"));
Exception rootCause = ExceptionUtil.rootCause(e);
assertTrue(rootCause.getMessage().contains("invalid input syntax for type json"));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.vladmihalcea.hibernate.type.json.internal;

import com.fasterxml.jackson.databind.JsonNode;
import org.hibernate.type.descriptor.ValueBinder;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
Expand All @@ -22,13 +21,13 @@ public <X> ValueBinder<X> getBinder(final JavaTypeDescriptor<X> javaTypeDescript
return new BasicBinder<X>(javaTypeDescriptor, this) {
@Override
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
st.setObject(index, javaTypeDescriptor.unwrap(value, JsonNode.class, options), getSqlType());
st.setObject(index, javaTypeDescriptor.unwrap(value, String.class, options), getSqlType());
}

@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
st.setObject(name, javaTypeDescriptor.unwrap(value, JsonNode.class, options), getSqlType());
st.setObject(name, javaTypeDescriptor.unwrap(value, String.class, options), getSqlType());
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.vladmihalcea.hibernate.type.json;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonNode;
import com.vladmihalcea.hibernate.util.AbstractPostgreSQLIntegrationTest;
import com.vladmihalcea.hibernate.util.ExceptionUtil;
Expand Down Expand Up @@ -152,8 +151,8 @@ public void testInvalidJson() {
});
fail("An invalid JSON should throw an exception!");
} catch (Exception e) {
JsonParseException rootCause = ExceptionUtil.rootCause(e);
assertTrue(rootCause.getMessage().contains("Unexpected character (':'"));
Exception rootCause = ExceptionUtil.rootCause(e);
assertTrue(rootCause.getMessage().contains("invalid input syntax for type json"));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.vladmihalcea.hibernate.type.json.internal;

import com.fasterxml.jackson.databind.JsonNode;
import org.hibernate.type.descriptor.ValueBinder;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.JavaType;
Expand All @@ -22,13 +21,13 @@ public <X> ValueBinder<X> getBinder(final JavaType<X> javaType) {
return new BasicBinder<X>(javaType, this) {
@Override
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
st.setObject(index, javaType.unwrap(value, JsonNode.class, options), getJdbcTypeCode());
st.setObject(index, javaType.unwrap(value, String.class, options), getJdbcTypeCode());
}

@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
st.setObject(name, javaType.unwrap(value, JsonNode.class, options), getJdbcTypeCode());
st.setObject(name, javaType.unwrap(value, String.class, options), getJdbcTypeCode());
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.vladmihalcea.hibernate.type.json;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonNode;
import com.vladmihalcea.hibernate.util.AbstractPostgreSQLIntegrationTest;
import com.vladmihalcea.hibernate.util.ExceptionUtil;
Expand Down Expand Up @@ -164,8 +163,8 @@ public void testInvalidJson() {
});
fail("An invalid JSON should throw an exception!");
} catch (Exception e) {
JsonParseException rootCause = ExceptionUtil.rootCause(e);
assertTrue(rootCause.getMessage().contains("Unexpected character (':'"));
Exception rootCause = ExceptionUtil.rootCause(e);
assertTrue(rootCause.getMessage().contains("invalid input syntax for type json"));
}
}

Expand Down

0 comments on commit a2f92c0

Please sign in to comment.