I had little different issue, I was facing this issue for pagination. When select query was pulling number of records (15) more than pageSize(10) then I was getting below error. -
Details: org.springframework.orm.jpa.JpaSystemException: No Dialect mapping for JDBC type: 1111; nested exception is org.hibernate.MappingException: No Dialect mapping for JDBC type: 1111
Before fixing I made sure -
- My Dialect lib is latest
- column data types are correct
Fix - added registerHibernateType
@Component
public class JSONBPostgreSQLDialect extends PostgreSQL95Dialect {
public JSONBPostgreSQLDialect() {
super();
registerColumnType(Types.JAVA_OBJECT, JSONBUserType.JSONB_TYPE);
//This line fixed Dialect mapping errorx
registerHibernateType(Types.OTHER, String.class.getName());
}
}
JSONBUserType
is custom type created.
@Component
public class JSONBUserType implements UserType, ParameterizedType {
private static final ClassLoaderService classLoaderService = new ClassLoaderServiceImpl();
public static final String JSONB_TYPE = "jsonb";
public static final String CLASS = "CLASS";
private Class jsonClassType;
@Override
public Class<Object> returnedClass() {
return Object.class;
}
@Override
public int[] sqlTypes() {
return new int[]{Types.JAVA_OBJECT};
}
@Override
public Object nullSafeGet(ResultSet rs,
String[] names,
SharedSessionContractImplementor session,
Object owner)
throws HibernateException, SQLException {
final String cellContent = rs.getString(names[0]);
if (cellContent == null) {
return null;
}
try {
final ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(cellContent.getBytes("UTF-8"), jsonClassType);
} catch (final Exception ex) {
throw new RuntimeException("Failed to convert String to Invoice: " + ex.getMessage(), ex);
}
}
@Override
public void nullSafeSet(PreparedStatement ps,
Object value,
int idx,
SharedSessionContractImplementor session)
throws HibernateException, SQLException {
if (value == null) {
ps.setNull(idx,
Types.OTHER);
return;
}
try {
final ObjectMapper mapper = new ObjectMapper();
final StringWriter w = new StringWriter();
mapper.writeValue(w,
value);
w.flush();
ps.setObject(idx,
w.toString(),
Types.OTHER);
} catch (final Exception ex) {
throw new RuntimeException("Failed to convert Invoice to String: " + ex.getMessage(),
ex);
}
}
@Override
public void setParameterValues(Properties parameters) {
final String clazz = (String) parameters.get(CLASS);
if (null != clazz) {
jsonClassType = classLoaderService.classForName(clazz);
}
}
@SuppressWarnings("unchecked")
@Override
public Object deepCopy(Object value) throws HibernateException {
if (!(value instanceof Collection)) {
return value;
}
Collection<?> collection = (Collection) value;
Collection collectionClone = CollectionFactory.newInstance(collection.getClass());
collectionClone.addAll(collection.stream()
.map(this::deepCopy)
.collect(Collectors.toList()));
return collectionClone;
}
static final class CollectionFactory {
@SuppressWarnings("unchecked")
static <E, T extends Collection<E>> T newInstance(Class<T> collectionClass) {
if (List.class.isAssignableFrom(collectionClass)) {
return (T) new ArrayList<E>();
} else if (Set.class.isAssignableFrom(collectionClass)) {
return (T) new HashSet<E>();
} else {
throw new IllegalArgumentException("Unsupported collection type : " + collectionClass);
}
}
}
@Override
public boolean isMutable() {
return true;
}
@Override
public boolean equals(Object x,
Object y) throws HibernateException {
if (x == y) {
return true;
}
if ((x == null) || (y == null)) {
return false;
}
return x.equals(y);
}
@Override
public int hashCode(Object x) throws HibernateException {
assert (x != null);
return x.hashCode();
}
@Override
public Object assemble(Serializable cached,
Object owner) throws HibernateException {
return deepCopy(cached);
}
@Override
public Serializable disassemble(Object value) throws HibernateException {
Object deepCopy = deepCopy(value);
if (!(deepCopy instanceof Serializable)) {
throw new SerializationException(String.format("%s is not serializable class",
value),
null);
}
return (Serializable) deepCopy;
}
@Override
public Object replace(Object original,
Object target,
Object owner) throws HibernateException {
return deepCopy(original);
}
}
And it is used in entity like this
@TypeDef(name = "addressValType", typeClass = JSONBUserType.class, parameters = {@Parameter(name = JSONBUserType.CLASS, value = "com.address.response.AddressResponse")})
@Table(name = "addressValidation")
public class Address implements Serializable {
private static final long serialVersionUID = -2370572023911224797L;
@Id
@Column(name = "employeeNumber")
private Integer employeeNumber;
@Column(name = "inputAddress", columnDefinition = "jsonb")
@Type(type = "addressValType")
private SapPostalAddressResponse inputAddress;
}
No Dialect mapping for JDBC type: 1111
indicates java.sql.Types.OTHER`. What column types do you select from the table? – Lucindalucine