Re: Question: can Cayenne generate the DAO classes by modeler?

From: Mike Kienenberger (mkienen..mail.com)
Date: Mon Aug 24 2009 - 09:10:45 EDT

  • Next message: Null kühl: "Simple Select Query"

    2009/8/23 xiaoxin xiaoxin <fxfx_00..otmail.com>:
    > Thanks for your quick response. By the way, would you
    > like to share your DAO template to me for study and teach me how to
    > import it to the modeler?

    My DAO template is part of a more complicated system that also has
    vertical inheritance, interfaces for entities, and a parallel mock
    class structure.

    I can send it to you, but I think it's likely to be too complex for
    what you want, nor is it likely to be exactly how you want to
    implement DAOs. It's also based on Cayenne 1.2. Actually, this
    particular example is probably based on Cayenne 1.1. There's a lot
    more flexibility and options available under later versions of
    Cayenne. If you still need help later, I will see if I can send you
    my 1.2 examples.

    Instead, let me give you some of the basics -- you can then write
    exactly what you need. Rather than using it in the modeler, I use ant
    tasks as this used to give you a lot more flexibility (that may no
    longer be true under Cayenne 3).

        <target name="generate dao" depends="" description="cmd-line:
    Rebuild classes dependent on database model">
                    <taskdef name="cgen"
    classname="org.objectstyle.cayenne.tools.CayenneGenerator">
                         <classpath refid="generate.classpath"/>
                    </taskdef>

                    <cgen map="${model.dir}/MyApp.map.xml"
                            entity="Account"
                              filenamePrefix=""
                              filenameSuffix="_DAOCayenne.java"
                              overwrite="true"
                          destDir="${src.dir}/com/xyz/applications/epay/cayenne"
                          makepairs="false"
                           template
    ="${templates.dir}/ModelerTemplates/_CayenneDataMapDAO.java.vm"
                          usepkgpath="false"/>
        </target>

    =======================

    #set ($objEntityList = $classGen.Entity.DataMap.ObjEntities)
    ###
    ###
    package com.xyz.applications.myApp.cayenne;

    import java.util.List;

    import org.objectstyle.cayenne.access.DataContext;

    import com.xyz.applications.myApp._MyAppDAO;
    ##
    #foreach ($objEntity in $objEntityList)
    import com.xyz.cayenne.ebpp.entity.${objEntity.Name};
    #end## ($objEntity in $objEntityList)
    ##
    import com.xyz.utilities.entity.DataAccessException;

    public class _MyAppDAOCayenne extends BaseDAOCayenne implements _MyAppDAO
    {
        public _MyAppDAOCayenne(DataContext dataContext, User
    administrativeUser, User selectedUser) {
            super(dataContext, administrativeUser, selectedUser);
        }
    #foreach ($objEntity in $objEntityList)

        public List<${objEntity.Name}> get${objEntity.Name}List() throws
    DataAccessException {
            return (List<${objEntity.Name}>)getAllObjects(${objEntity.Name}.class);
        }
    #if (! $objEntity.isReadOnly())

        public ${objEntity.Name} create${objEntity.Name}() {
                    return (${objEntity.Name})${objEntity.Name}.createAndInsertInstance(getDataContext());
        }

        public void delete${objEntity.Name}(${objEntity.Name} object)
    throws DataAccessException {
            object.deleteObject(getAdministrativeUser(), getSelectedUser());
        }
    #end##if ($objEntity.isReadOnly())
    ##
        public ${objEntity.Name} find${objEntity.Name}ByPrimaryKey(Object
    primaryKey) throws DataAccessException
        {
                ${objEntity.Name} matching${objEntity.Name} =
    (${objEntity.Name})objectMatchingPrimaryKey(getDataContext(),
    ${objEntity.Name}.class, primaryKey);
                return matching${objEntity.Name};
        }

    #end## ($objEntity in $objEntityList)
    }



    This archive was generated by hypermail 2.0.0 : Mon Aug 24 2009 - 09:11:52 EDT