I have a bean class
public class Group{string name;Type type; }
and another bean
public class Type{String name;}
Now, i want to bind group by using jdbi @BindBean
@SqlBatch("INSERT INTO (type_id,name) VALUES((SELECT id FROM type WHERE name=:m.type.name),:m.name)")
@BatchChunkSize(100)
int[] insertRewardGroup(@BindBean ("m") Set<Group> groups);
How can i bind the user defined object's property as member of the bean??
You could implement your own Bind-annotation here. I implemented one that I am adopting for this answer. It will unwrap all Type ones. 
I think it could be made fully generic with a little more work.
Your code would look like this (please note that m.type.name changed to m.type):
@SqlBatch("INSERT ... WHERE name=:m.type),:m.name)")
@BatchChunkSize(100)
int[] insertRewardGroup(@BindTypeBean ("m") Set<Group> groups);
This would be the annotation:
@BindingAnnotation(BindTypeBean.SomethingBinderFactory.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public @interface BindTypeBean {
    String value() default "___jdbi_bare___";
    public static class SomethingBinderFactory implements BinderFactory {
        public Binder build(Annotation annotation) {
            return new Binder<BindTypeBean, Object>() {
                public void bind(SQLStatement q, BindTypeBean bind, Object arg) {
                    final String prefix;
                    if ("___jdbi_bare___".equals(bind.value())) {
                        prefix = "";
                    } else {
                        prefix = bind.value() + ".";
                    }
                    try {
                        BeanInfo infos = Introspector.getBeanInfo(arg.getClass());
                        PropertyDescriptor[] props = infos.getPropertyDescriptors();
                        for (PropertyDescriptor prop : props) {
                            Method readMethod = prop.getReadMethod();
                            if (readMethod != null) {
                                Object r = readMethod.invoke(arg);
                                Class<?> c = readMethod.getReturnType();
                                if (prop.getName().equals("type") && r instanceof Type) {
                                    r = ((Type) r).getType();
                                    c = r.getClass();
                                }
                                q.dynamicBind(c, prefix + prop.getName(), r);
                            }
                        }
                    } catch (Exception e) {
                        throw new IllegalStateException("unable to bind bean properties", e);
                    }
                }
            };
        }
    }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With