@UseStringTemplate3StatementLocator
@RegisterMapperFactory(BeanMapperFactory.class)
public interface UserDao {
@SqlQuery("select count(*) from user")
@Mapper(IntegerMapper.class)
int total();
@SqlQuery("select * from user where id = :id") User findById(@Bind("id") String id);
}
You don't need to specify a mapper for built in types--JDBI matches them up out of the box for you.
--
You received this message because you are subscribed to the Google Groups "jDBI" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jdbi+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
java.lang.IllegalArgumentException: A bean, int, was mapped which was not instantiable at com.peptalk.db.BeanMapper.map(BeanMapper.java:56) at org.skife.jdbi.v2.RegisteredMapper.map(RegisteredMapper.java:35) at org.skife.jdbi.v2.Query$4.munge(Query.java:183) at org.skife.jdbi.v2.QueryResultSetMunger.munge(QueryResultSetMunger.java:41) at org.skife.jdbi.v2.SQLStatement.internalExecute(SQLStatement.java:1344) at org.skife.jdbi.v2.Query.fold(Query.java:173) at org.skife.jdbi.v2.Query.first(Query.java:273) at org.skife.jdbi.v2.Query.first(Query.java:264) at org.skife.jdbi.v2.sqlobject.ResultReturnThing$SingleValueResultReturnThing.result(ResultReturnThing.java:110) at org.skife.jdbi.v2.sqlobject.ResultReturnThing.map(ResultReturnThing.java:46) at org.skife.jdbi.v2.sqlobject.QueryHandler.invoke(QueryHandler.java:43) at org.skife.jdbi.v2.sqlobject.SqlObject.invoke(SqlObject.java:215) at org.skife.jdbi.v2.sqlobject.SqlObject$3.intercept(SqlObject.java:127) at org.skife.jdbi.v2.sqlobject.CloseInternalDoNotUseThisClass$$EnhancerByCGLIB$$3dea96bc.findAllTotal(<generated>) at com.peptalk.service.PerpService.findAllTotal(PerpService.java:88) at com.peptalk.controller.api.PerpsController.searchPerp(PerpsController.java:56) at com.peptalk.controller.api.PerpsController$$FastClassBySpringCGLIB$$42f471ea.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:717) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85) at com.peptalk.aop.CheckInAspect.aroundCall(CheckInAspect.java:117) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621) at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610) at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:68) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)
at com.peptalk.controller.api.PerpsController$$EnhancerBySpringCGLIB$$40a42382.searchPerp(<generated>) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:806) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:729) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166) at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:129) at com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:77) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61) at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108) at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137) at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125) at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66) at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449) at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365) at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90) at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83) at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383) at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362) at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125) at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262) at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157) at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388) at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182) at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765) at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:440) at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230) at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114) at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) at org.mortbay.jetty.Server.handle(Server.java:326) at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542) at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:926) at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549) at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212) at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404) at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410) at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)Caused by: java.lang.InstantiationException: int at java.lang.Class.newInstance(Class.java:364) at com.peptalk.db.BeanMapper.map(BeanMapper.java:54) ... 91 more
To unsubscribe from this group and stop receiving emails from it, send an email to jdbi+uns...@googlegroups.com.
> To unsubscribe from this group and stop receiving emails from it, send an email to jdbi+unsubscribe@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "jDBI" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jdbi+unsubscribe@googlegroups.com.
/**
* A result set mapper which maps the fields in a statement into a JavaBean.
* This uses the JDK's built in bean mapping facilities, so it does not support
* nested properties.
*/
public class BeanMapper<T> implements ResultSetMapper<T> {
private final Class<T> type;
private final Map<String, PropertyDescriptor> properties = new HashMap<String, PropertyDescriptor>();
public BeanMapper(Class<T> type) {
this.type = type;
try {
BeanInfo info = Introspector.getBeanInfo(type);
for (PropertyDescriptor descriptor : info.getPropertyDescriptors()) {
properties.put(descriptor.getName().toLowerCase(), descriptor);
}
} catch (IntrospectionException e) {
throw new IllegalArgumentException(e);
}
}
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public final T map(int row, ResultSet rs, StatementContext ctx)
throws SQLException {
T bean;
try {
bean = type.newInstance();
} catch (Exception e) {
throw new IllegalArgumentException(String.format(
"A bean, %s, was mapped " + "which was not instantiable",
type.getName()), e);
}
ResultSetMetaData metadata = rs.getMetaData();
for (int i = 1; i <= metadata.getColumnCount(); ++i) {
String name = metadata.getColumnLabel(i).toLowerCase()
.replaceAll("_", "");
PropertyDescriptor descriptor = properties.get(name);
if (descriptor != null) {
Class propertyType = descriptor.getPropertyType();
Object value;
if (propertyType.isAssignableFrom(Boolean.class)
|| propertyType.isAssignableFrom(boolean.class)) {
value = rs.getBoolean(i);
} else if (propertyType.isAssignableFrom(Byte.class)
|| propertyType.isAssignableFrom(byte.class)) {
value = rs.getByte(i);
} else if (propertyType.isAssignableFrom(Short.class)
|| propertyType.isAssignableFrom(short.class)) {
value = rs.getShort(i);
} else if (propertyType.isAssignableFrom(Integer.class)
|| propertyType.isAssignableFrom(int.class)) {
try {
value = rs.getInt(i);
} catch (Exception e) {
value = 0;
System.out.println(metadata.getColumnLabel(i));
}
} else if (propertyType.isAssignableFrom(Long.class)
|| propertyType.isAssignableFrom(long.class)) {
value = rs.getLong(i);
} else if (propertyType.isAssignableFrom(Float.class)
|| propertyType.isAssignableFrom(float.class)) {
value = rs.getFloat(i);
} else if (propertyType.isAssignableFrom(Double.class)
|| propertyType.isAssignableFrom(double.class)) {
value = rs.getDouble(i);
} else if (propertyType.isAssignableFrom(BigDecimal.class)) {
value = rs.getBigDecimal(i);
} else if (propertyType.isAssignableFrom(Timestamp.class)) {
value = rs.getTimestamp(i);
} else if (propertyType.isAssignableFrom(Time.class)) {
value = rs.getTime(i);
} else if (propertyType.isAssignableFrom(Date.class)) {
value = rs.getDate(i);
} else if (propertyType.isAssignableFrom(String.class)) {
value = rs.getString(i);
} else if (propertyType.isEnum()) {
value = Enum.valueOf(propertyType, rs.getString(i));
} else {
value = rs.getObject(i);
}
if (rs.wasNull() && !propertyType.isPrimitive()) {
value = null;
}
try {
descriptor.getWriteMethod().invoke(bean, value);
} catch (IllegalAccessException e) {
throw new IllegalArgumentException(String.format(
"Unable to access setter for " + "property, %s",
name), e);
} catch (InvocationTargetException e) {
throw new IllegalArgumentException(String.format(
"Invocation target exception trying to "
+ "invoker setter for the %s property",
name), e);
} catch (NullPointerException e) {
throw new IllegalArgumentException(String.format(
"No appropriate method to " + "write property %s",
name), e);
}
}
}
return bean;
}
}
That looks like a copy of JDBI's BeanMapper. What does your BeanMapperFactory class look like?
To unsubscribe from this group and stop receiving emails from it, send an email to jdbi+unsubscribe@googlegroups.com.
@SuppressWarnings({ "rawtypes", "unchecked" })
public class BeanMapperFactory implements ResultSetMapperFactory {
@Override
public final boolean accepts(Class type, StatementContext ctx) {
return true;
}
@Override
public final ResultSetMapper mapperFor(Class type, StatementContext ctx) {
return new BeanMapper(type);
}
}
Yep, that would do it, that's an old implementation that naively assumes it can map anything.
If you're using a recent version, I suggest using the BeanMapperFactory from JDBI instead of the com.peptalk one.
To unsubscribe from this group and stop receiving emails from it, send an email to jdbi+unsubscribe@googlegroups.com.