ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ingmar Lötzsch <iloetz...@asci-systemhaus.de>
Subject Re: IBatis Array feild insertion problem in postgres database()Please help me)
Date Thu, 08 Oct 2009 11:48:40 GMT
You can use a type handler and implement java.sql.Array. Example:

1st: abstract superclass as adapter

public abstract class SqlArrayAdapter
implements Array
{
	public Object getArray() throws SQLException
	{
		// Auto-generated method stub
		return null;
	}
	
	public Object getArray(long index, int count) throws SQLException
	{
		// Auto-generated method stub
		return null;
	}
	
	public Object getArray(long index, int count, Map<String, Class< ? >>
map) throws SQLException
	{
		// Auto-generated method stub
		return null;
	}
	
	public Object getArray(Map<String, Class< ? >> map) throws SQLException
	{
		// Auto-generated method stub
		return null;
	}
	
	public int getBaseType() throws SQLException
	{
		// Auto-generated method stub
		return 0;
	}
	
	public String getBaseTypeName() throws SQLException
	{
		// Auto-generated method stub
		return null;
	}
	
	public ResultSet getResultSet() throws SQLException
	{
		// Auto-generated method stub
		return null;
	}
	
	public ResultSet getResultSet(long index, int count) throws SQLException
	{
		// Auto-generated method stub
		return null;
	}
	
	public ResultSet getResultSet(long index, int count, Map<String, Class<
? >> map) throws SQLException
	{
		// Auto-generated method stub
		return null;
	}
	
	public ResultSet getResultSet(Map<String, Class< ? >> map) throws
SQLException
	{
		// Auto-generated method stub
		return null;
	}
}

2nd: implementation of java.sql.Array for use with int[] or
Collection<Integer>

public class IntArray
extends SqlArrayAdapter
{
	private static final Integer[] emptyArray = new Integer[0];
	
	private int[] array;
	
	public IntArray(int[] array)
	{
		if (array == null)
		{
			throw new IllegalArgumentException("parameter array should not be null");
		}
		this.array = array;
	}
	
	public IntArray(Collection<Integer> set)
	{
		if (set == null)
		{
			throw new IllegalArgumentException("parameter set should not be null");
		}
		Integer[] keys = set.toArray(emptyArray);

		this.array = new int[keys.length];
		for (int i = 0; i < keys.length; ++i)
		{
			Integer key = keys[i];
			this.array[i] = key.intValue();
		}
	}
	
	@Override
	public int getBaseType()
//	throws SQLException
	{
		return Types.INTEGER;
	}

	/**
	 * This method is called by driver ver. 8 but not by ver. 7.
	 */
	@Override
	public String getBaseTypeName()
//	throws SQLException
	{
		return "int4";
	}
	
	/**
	 * This method is called by both drivers ver. 8 and 7.
	 */
	@Override
	public String toString()
	{
		String result = "{";
		for (int i = 0; i < this.array.length; ++i)
		{
			if (i > 0)
			{
				result += ",";
			}
			result += this.array[i];
		}
		result += "}";
		return result;
	}
	
	public void free()
//	throws SQLException
	{
		this.array = null;
	}
}

3rd: implementation of TypeHandlerCallback

public class IntArrayTypeHandler
implements TypeHandlerCallback
{
	public void setParameter(ParameterSetter setter, Object parameter)
	throws SQLException
	{
		Collection<Integer> keys = (Collection<Integer>) parameter;
		IntArray intArray = new IntArray(keys);
		setter.setArray(intArray);
	}
	
	public Object getResult(ResultGetter getter)
	throws SQLException
	{
		Array array = getter.getArray();
		return array;
	}
	
	public Object valueOf(String string)
	{
		return string;
	}
}

4th: add the type handler to your configuration
	<typeAlias alias="IntArrayTypeHandler"
type="com.asci.common.ibatis.IntArrayTypeHandler" />

5th: SQL map

<select id="selectFahrgastById" parameterClass="map" resultMap="...">
	SELECT
		k.id,
		...
	FROM kunden AS k
	WHERE k.id = ANY (#fahrgastIds,handler=IntArrayTypehandler#)
</select>

6th: DAO

public List<Fahrgast> selectFahrgastById(Set<Integer> fahrgastIds)
{
	HashMap<String, Object> params = new HashMap<String, Object>();
	params.put("fahrgastIds", fahrgastIds);
	List<Fahrgast> list = getSqlMapClientTemplate().queryForList(
		"datenerfassung.selectFahrgastById", params);
	return list;
}

jishnu123 schrieb:
> 
>    Hai,
> 
>          I have to tried insertion of array feild in postgres database using
> spring mvc with iBatis.I got two lines error ...like 
> 
>                                 Error as follows...
> 
> 
> [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - <Loading
> XML bean definitions from class path resource
> [org/springframework/jdbc/support/sql-error-codes.xml]>
>  [org.springframework.jdbc.support.SQLErrorCodesFactory] - <SQLErrorCodes
> loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL,
> Sybase]>
> 
> 
>    Its urgent ....Please help me........Any one has an idea  please  reply
> immediately otherwise sent to my emaild id rjishnu@gmail.com 
>       
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
For additional commands, e-mail: user-java-help@ibatis.apache.org


Mime
View raw message