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 人气:262
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: 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

  • <J2EE Development without EJB> study notes (4) 2010-05-27

    Introduction Chapter 7, Spring framework (IOC) Lightweight container is an important starting point is the elimination of many J2EE applications, home-made factories and Singleton. For service-type object, Singleton pattern is sufficient. Factory Bea

  • 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

  • Domain driven design and development of real 2010-09-29 Background Domain Driven Design (DDD) is central to the concept of how the business is mapped to software artifacts. Most books on the subject and articles to Eric Evans book "Domain Driven Design"

  • 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

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



Copyright (C), All Rights Reserved. 版权所有 闽ICP备15018612号

processed in 0.063 (s). 10 q(s)