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

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


                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 =;
                        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 人气:249
blog comments powered by Disqus


  • MySQL-Proxy load balance test and analysis of problems & & MySQL-Proxy working mechanism 2010-12-23

    (1) MySQL-Proxy load balancing problems encountered in testing and analysis (A) in the 0.8.1 version of mysql-proxy load balancing test, the test results appear as follows: First start mysql-proxy: [Root @ rap2 ~] # mysqlslap-h 4040-u roo

  • MySQL-Proxy load balancing problems and analysis of test & & MySQL-Proxy working mechanism 2010-12-23

    (1) MySQL-Proxy load balancing problems encountered in testing and analysis (A) in 0.8.1 version of mysql-proxy load balancing test, the test results appear as follows: First start mysql-proxy: [Root @ rap2 ~] # mysqlslap-h 4040-u root-pc

  • hyk-proxy built on top of a high-performance web proxy GAE 2010-03-29

    hyk-proxy reference About hyk-proxy The preparation of a Java-based RPC mechanism to bring about the web proxy, running on Google AppEngine platform Support HTTP / XMPP as the communication protocol Open-source project, hosted on Google Code ( http:/

  • 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: 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

  • could not initialize proxy - no Session 2010-09-02

    Original Address: In <many-to-on> of this error is common, literally just can not be initialized because the session was closed. Easy to understand because, you use the lazy = true, In this way hibernat

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



Copyright (C), All Rights Reserved. 版权所有 黔ICP备15002463号-1

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