Nutz DAO lazy loading entities associated with the object

Previously been using Hibernate, accustomed to the lazy loading of related entities. Recently used Nutz, which did not provide support for lazy loading, and thus be expanded about their own code to support lazy loading Dao. So users do not need to concern for data capture from the dao object associated with a question, call the POPO's getter method can automatically load related entities. The following specific program in order as follows:
1. Define a method called LinkFieldGetterIntercepter interceptor: the getter for the call to POJO automatically load related objects.
2. From the definition of a EntityHolder: reloadEntity entity created when the proxy class, add the definition in Article 1 of the interceptor. And using entity mappings in the cache proxy class in the entity.
3. From the definition of a org.nutz.dao.entity.Borning: easy when the ResultSet into a POJO, POJO to create the proxy class instance, not the original POJO class entity.
4. Since the definition of a EntityMaker: make analytical method entity setBorning (...) is defined in section 3 Borning object.

Here I use JPA as the POJO's annotation, extended the relevant code, if used directly Nutz annotation should be easier to achieve. Method is the same.
1. LinkFieldGetterIntercepter:

public class LinkFieldGetterIntercepter extends AbstractMethodInterceptor {
        private Dao dao;
        private Link link;

        public LinkFieldGetterIntercepter(Dao dao, Link link) {
                this.dao = dao;
                this.link = link;
        }

        public Object afterInvoke(Object obj, Object returnObj, Method method, Object... args) {
                //  You can determine whether to returnObj  null, To determine whether it needs to load the associated entity, after loading the associated entities writeback  obj The associated fields
                if (link.isOne()) {
......
                } else if (link.isMany()) {

......
                } else if (link.isManyMany()) {

......
                }
                Mirrors.setValue(obj, link.getOwnField().getName(), returnObj);
                return returnObj;
        }
}

2. EntityHolder

public class DemsyEntityHolder {
        private Dao dao;// Dao  Object
        private EntityMaker maker;
        private Map<Class<?>, Entity<?>> mappings;// <AOP The proxy class, an entity  >
        private Map<Class, Class> agentClassMap;// < The entity classes, AOP proxy class  >

        public DemsyEntityHolder(DemsyNutDao dao) {
                this.dao = dao;
                this.maker = dao.getEntityMaker();
                mappings = new HashMap<Class<?>, Entity<?>>();
                agentClassMap = new HashMap<Class, Class>();
        }

        public <T> Entity<T> getEntity(Class<T> classOfT) {
                //  Converted to AOP proxy class
                if (!Mirrors.isAgent(classOfT)) {
                        classOfT = agentClassMap.get(classOfT);
                }
                if (classOfT == null) {
                        return null;
                }

                return (Entity<T>) mappings.get(classOfT);
        }

        @SuppressWarnings("unchecked")
        public <T> Entity<T> reloadEntity(Class<T> classOfT, boolean autoCreateTable) {
                JPAEntity<?> entity = (JPAEntity<?>) maker.make(null, null, classOfT);

                //  Cached entity
                mappings.put(agentClass(entity), entity);

                return (Entity<T>) entity;
        }

        //  Create a proxy class
        private Class agentClass(JPAEntity entity) {
                Class cls = entity.getType();
                if (Mirrors.isAgent(cls)) {
                        return cls;
                }

                ClassAgent classAgent = new AsmClassAgent();
                List<Link> links = entity.getLinks(null);
                if (links != null) {
                        for (Link link : links) {
                                String fieldName = link.getOwnField().getName();
                                fieldName = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
                                classAgent.addInterceptor(MethodMatcherFactory.matcher("get" + fieldName),
                                                new LinkFieldGetterIntercepter(dao, link));
                        }
                }
                Class agentClass = classAgent.define(new DefaultClassDefiner(), cls);

                entity.setAgentMirror(Mirror.me(agentClass));

                this.agentClassMap.put(cls, agentClass);

                return agentClass;
        }

        public int count() {
                return mappings.size();
        }
}

3 Borning:

class DemsyBorning implements Borning {
        JPAEntity entity;

        DemsyBorning(JPAEntity entity) {
                this.entity = entity;
        }

        /**
         *  Create an instance of the entity agent
         *
         * @return
         * @throws Exception
         */
        public Object create() throws Exception {
                // Create an instance of the proxy class, instead of the original instance of the class
                return entity.getAgentMirror().born();
        }

        public Object born(ResultSet rs, FieldMatcher fm) throws Exception {
                Object obj = create();
                Iterator<EntityField> it = entity.fields().iterator();
                while (it.hasNext()) {
                        EntityField ef = it.next();
                        if (null == fm || fm.match(ef.getField().getName()))
                                ef.fillValue(obj, rs);
                }
                return obj;
        }
}

4 EntityMaker:

......
public Entity<?> make(DatabaseMeta db, Connection conn, Class<?> type) {
......
                // Borning
                entity.setBorning(new DemsyBorning(entity));
......
分类:Java 时间:2010-04-23 人气:250
分享到:
blog comments powered by Disqus

相关文章

  • struts2 interceptor (JDK reflection, JDK dynamic proxy) 2010-08-17

    The significance of the interceptor: As we all know, DRY (Don't Repeat Yourself, Do not write repetitive code) rules the software development process is an important law, to comply with the rules of the system will be developed with better maintainab

  • JDK dynamic proxy 2010-03-29

    1. Using proxy mode to achieve agent (non-dynamic proxy) In order to understand what is a dynamic agency, we must first look at the static agent approach. Whether that agent approach, there is proxy object and the target audience two models, the so-c

  • Struts2 Interceptor (Interceptor) 2010-03-29

    1. Understand the interceptor 1.1. What is the interceptor: Interceptors, in the AOP (Aspect-Oriented Programming) is used in a method or field has been visited before, before or after the block and then adding some operations. AOP interception is an

  • [Change] STRUTS2 Interceptor 2010-03-30

    Original link: http://hi.baidu.com/annleecn/blog/item/1b4931f2b7f2cb56352acca2.html Struts2 interceptor is an important part, Struts2 interceptor framework is dynamically configurable, following the first look at one of the most use JDK's reflection

  • Java JDK dynamic proxy agent of 2010-07-28

    In Java, the dynamic proxy is generated proxy class runs in the period, according to the proxy class to be dynamically generated proxy class file byte code (class files), and loaded into the operating environment, the same as other classes use the su

  • Layman CGlib-create the proxy classes without invasion 2010-12-06

    CGlib What is it? CGlib is a powerful, high performance, high quality Code generation library. It can run Java classes and implement Java extension interface. Of course, these features are actually provided by the asm, asm what is it? Java bytecode m

  • Use native SQL query JPQL and JPA entities (rpm) 2010-12-30

    Of: Yuli Vasiliev Learn how to use the Java Persistence query language and native SQL query JPA entities. Published September 2008 In the Java EE, Java Persistence API (JPA) is a standard for accessing relational database API, which provides a simple

  • Simple implementation Struts2 interceptor function simulation code 2010-03-29

    TargerInterface: (target interface, the definition of method) package com.interceptor; public interface TargerInterface { public void doSomething(); } Target: (target class to achieve method) package com.interceptor; public class Target implements Ta

  • CAS 3.x Proxy Configuration 2010-03-29

    http://fallenlord.blogbus.com/logs/57175888.html Recently modified system, need to do CAS's Proxy, surf the Internet for a long time, and found some articles about the principles, but almost no articles about the configuration, there is about the con

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.208 (s). 12 q(s)