custom db type in PostgreSql with Sql Mapper

61 views
Skip to first unread message

kemal.ediz

unread,
Jun 21, 2011, 11:08:43 AM6/21/11
to mybati...@googlegroups.com
Hi,

I've created a custom db type in PostgreSql like below:

=================

CREATE TYPE usertype as(
id INTEGER ,
username VARCHAR(100) ,
password VARCHAR(50) ,
firstName VARCHAR(50) ,
lastName VARCHAR(50)
);
=================

I am using the above custom type in a stored procedure which has a signature
like below:

=================

-- Procedure that inserts a new user record into the 'users' table with
multiple roles sent as an array parameter.
CREATE OR REPLACE FUNCTION save_user(
p_insert BOOLEAN, p_user usertype)
RETURNS integer AS $$
DECLARE

..........................................

END;
$$
LANGUAGE plpgsql;

=================

I have the following sql mapper method:

=================

String SQL_INSERT_USER = "{call save_user(" +
"true," +

"#{user,jdbcType=STRUCT,javaType=com.xxx.domain.User,typeHandler=com.xxx.mapper.UserTypeHandler})}";

@Select(SQL_INSERT_USER)
@Options(statementType=StatementType.CALLABLE)
Integer insert(Parameter param);
=================

The UserTypeHandler is like below:

=================

public class UserTypeHandler implements TypeHandler {

public void setParameter( PreparedStatement ps, int i, Object
parameter,JdbcType jdbcType) throws SQLException {
ps.setObject(i, (User) parameter);
}

public Object getResult( ResultSet rs, String columnName) throws
SQLException {
return rs.getObject(columnName);
}

public Object getResult( CallableStatement cs, int columnIndex) throws
SQLException {
return cs.getObject(columnIndex);
}
}
=================

The Parameter class is as follows:

=================
public class Parameter {
private User user;

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

}

=================

When I run this sql mapper method, I am getting the following exception:

=================
Cause: org.postgresql.util.PSQLException: Can't infer the SQL type to use
for an instance of com.sky.news.cms.admin.domain.User. Use setObject() with
an explicit Types value to specify the type to use.
=================

I think the problem is either in sql mapper annotation (SQL_INSERT_USER) or
in UserTypeHandler. Any ideas?

--
View this message in context: http://mybatis-user.963551.n3.nabble.com/custom-db-type-in-PostgreSql-with-Sql-Mapper-tp3091124p3091124.html
Sent from the mybatis-user mailing list archive at Nabble.com.

Reply all
Reply to author
Forward
0 new messages