com.dieselpoint.norm.converter.IntArrayToListConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of norm Show documentation
Show all versions of norm Show documentation
An extremely lightweight data access layer over JDBC
package com.dieselpoint.norm.converter;
import java.sql.Array;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import com.dieselpoint.norm.DbException;
@Converter
public class IntArrayToListConverter implements AttributeConverter, java.sql.Array> {
@Override
public Array convertToDatabaseColumn(List attribute) {
return new SimpleArray(java.sql.Types.INTEGER, attribute.toArray());
}
@Override
public List convertToEntityAttribute(Array dbData) {
try {
if (dbData.getBaseType() != java.sql.Types.INTEGER) {
throw new DbException("Database is not returning an integer array");
}
Integer [] arr = (Integer[]) dbData.getArray();
List out = new ArrayList<>();
for (Integer i: arr) {
out.add(i);
}
return out;
} catch (SQLException e) {
throw new DbException(e);
}
}
}