All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dieselpoint.norm.converter.IntArrayToListConverter Maven / Gradle / Ivy

There is a newer version: 1.1
Show newest version
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);
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy