Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jdbi BindBean userdefined property of the bean(nested object)

Tags:

java

jdbi

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??

like image 653
Nandu Prajapati Avatar asked Jun 24 '15 14:06

Nandu Prajapati


1 Answers

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);
                    }


                }
            };
        }
    }
}
like image 103
ahus1 Avatar answered Sep 29 '22 14:09

ahus1