I'm not a big fan of DAO's either and when I saw this it got me
thinking so I tried the following. The methods/queries are not as
dynamic since it requires an Interface but Eclipse will write that for
you and after that you get code completion, type checking etc.

The parser is about as dumb as it gets and only supports query by all
or narrow by 1 property name. That could be improved. There is not
much error checking either.

public interface Query {
        
        public <T> List<T> all(Class  clazz);

        public <T> List<T> name(Class  clazz, String name);
}

public class QueryProxyHibernate implements InvocationHandler {
        Logger log;
        Session session;
        
        public QueryProxyHibernate(Logger serviceLog, Session session) {
                this.log = serviceLog;
                this.session = session;
        }

        public Object invoke(Object proxy, Method method, Object[] args)
                        throws Throwable {
                log.debug(method.getName());
                if ( method.getName().equals("all")) {
                        return session.createCriteria((Class) args[0]).list();
                }
                
                return session.createCriteria((Class) args[0])
                        .add( Restrictions.eq(method.getName(), args[1]))
                        .list();
        }
}

In my appModule

    public static Query buildQuery(Logger serviceLog, Session session)
    {
                Class[] proxyInterfaces = new Class[] { Query.class };
            Query query = (Query) 
Proxy.newProxyInstance(Query.class.getClassLoader(),
                proxyInterfaces,
                new QueryProxyHibernate(serviceLog,session));
        return query;
    }

To use

        @Inject
        private Query query;

        public List<Person> getPeople() {
                return query.name(Person.class, "Barry");
        }

        public List<Product> getProducts() {
                return query.all(Product.class);
        }

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to