Re: Getting complex queries in cayenne

From: Sami Mohammed (SMOHAMME..ndependenthealth.com)
Date: Fri May 06 2005 - 15:19:24 EDT

  • Next message: Michael Gentry (Yes, I'm a Contractor): "Enumerations ..."

    Hi All,

    I also have the same problem. when i tried using the method which andrus wrote in his last mail, but on the jsp page it is retriving number of rows, but not displaying content
    what I am doing wrong pls let me know.
    How to display result on jsp. O r i have to use another method.

    DataContext context = BasicServletConfiguration.getDefaultContext(request..getSession());
                                    String sql = "select"
                                    + " #result('u.USR_ID' 'String'),"
                                    + " #result('u.LAST_NAME' 'String'),"
                                    + " #result('t.SECURITY_SHORT_DESC' 'String'),"
                                    + " #result('u.LAST_LOGIN_DATE' 'java.util.Date')"
                                    + " from IHA.EDW_DATA_MGMT_SECURITY_MSTR t,"
                                    + "IHA.EDW_DATA_MGMT_SECURITY_USR u"
                                    + " where t.security_mstr_key=u.security_mstr_key";
                                    
                                    // get list of users
                                    
                                    SQLTemplate query = new SQLTemplate(EdwDataMgmtSecurityUsr.class, sql, true);
                                    //query.setTemplate(DbAdapter.ORACLE,sql);
                                    
                                    
                                    List resultList = context.performQuery(query);
                            
                                    if (!resultList.isEmpty())
                                    {
                                            // Store the results into the session object

                                            request.getSession().setAttribute("DisplayResult", resultList);
                                            
                                    }
                            }

    DataMap file below

    <?xml version="1.0" encoding="utf-8"?>
    <data-map project-version="1.1">
            <property name="defaultPackage" value="com.independenthealth.edwdma.admin.cayenne"/>
            <property name="defaultSchema" value="IHA"/>
            
            <db-entity name="EDW_DATA_MGMT_SECURITY_MSTR" schema="IHA">
                    <db-attribute name="ADMIN_FLAG" type="CHAR" length="1"/>
                    <db-attribute name="CW_SECURITY_ROLE_KEY" type="INTEGER" length="22"/>
                    <db-attribute name="ERR_SECURITY_ROLE_KEY" type="INTEGER" length="22"/>
                    <db-attribute name="LAST_CHNG_DATE" type="TIMESTAMP" length="7"/>
                    <db-attribute name="LAST_USR_KEY" type="INTEGER" length="22"/>
                    <db-attribute name="SECURITY_LONG_DESC" type="VARCHAR" length="255"/>
                    <db-attribute name="SECURITY_MSTR_KEY" type="INTEGER" isPrimaryKey="true" isMandatory="true" length="22"/>
                    <db-attribute name="SECURITY_SHORT_DESC" type="VARCHAR" length="70"/>
                    <db-key-generator>
                            <db-generator-type>ORACLE</db-generator-type>
                            <db-generator-name>EDW_MGMT_SECURITY_MSTR_SEQ</db-generator-name>
                            <db-key-cache-size>20</db-key-cache-size>
                    </db-key-generator>
            </db-entity>
            
            <db-entity name="EDW_DATA_MGMT_SECURITY_USR" schema="IHA">
                    <db-attribute name="ACTV_FLAG" type="CHAR" length="1"/>
                    <db-attribute name="FIRST_NAME" type="VARCHAR" length="30"/>
                    <db-attribute name="LAST_CHNG_DATE" type="TIMESTAMP" length="7"/>
                    <db-attribute name="LAST_LOGIN_DATE" type="TIMESTAMP" length="7"/>
                    <db-attribute name="LAST_NAME" type="VARCHAR" length="35"/>
                    <db-attribute name="LAST_USR_KEY" type="INTEGER" length="22"/>
                    <db-attribute name="PSSWD" type="VARCHAR" length="30"/>
                    <db-attribute name="SECURITY_MSTR_KEY" type="INTEGER" length="22"/>
                    <db-attribute name="USR_ID" type="VARCHAR" isMandatory="true" length="15"/>
                    <db-attribute name="USR_KEY" type="INTEGER" isPrimaryKey="true" isMandatory="true" length="22"/>
            </db-entity>

                    <obj-entity name="EdwDataMgmtSecurityMstr" className="com.independenthealth.edwdma.admin.cayenne.EdwDataMgmtSecurityMstr" dbEntityName="EDW_DATA_MGMT_SECURITY_MSTR">
                    <obj-attribute name="adminFlag" type="java.lang.String" db-attribute-path="ADMIN_FLAG"/>
                    <obj-attribute name="cwSecurityRoleKey" type="java.lang..Integer" db-attribute-path="CW_SECURITY_ROLE_KEY"/>
                    <obj-attribute name="errSecurityRoleKey" type="java.lang.Integer" db-attribute-path="ERR_SECURITY_ROLE_KEY"/>
                    <obj-attribute name="lastChngDate" type="java.util.Date" db-attribute-path="LAST_CHNG_DATE"/>
                    <obj-attribute name="lastUsrKey" type="java.lang.Integer" db-attribute-path="LAST_USR_KEY"/>
                    <obj-attribute name="securityLongDesc" type="java.lang.String" db-attribute-path="SECURITY_LONG_DESC"/>
                    <obj-attribute name="securityMstrKey" type="java.lang.Integer" db-attribute-path="SECURITY_MSTR_KEY"/>
                    <obj-attribute name="securityShortDesc" type="java.lang..String" db-attribute-path="SECURITY_SHORT_DESC"/>
            </obj-entity>
            
            <obj-entity name="EdwApplicSecurityUsr" className="com.independenthealth.edwdma.admin.cayenne.EdwApplicSecurityUsr" dbEntityName="EDW_DATA_MGMT_SECURITY_USR">
                    <obj-attribute name="actvFlag" type="java.lang.String" db-attribute-path="ACTV_FLAG"/>
                    <obj-attribute name="firstName" type="java.lang.String" db-attribute-path="FIRST_NAME"/>
                    <obj-attribute name="lastChngDate" type="java.util.Date" db-attribute-path="LAST_CHNG_DATE"/>
                    <obj-attribute name="lastLoginDate" type="java.util.Date" db-attribute-path="LAST_LOGIN_DATE"/>
                    <obj-attribute name="lastName" type="java.lang.String" db-attribute-path="LAST_NAME"/>
                    <obj-attribute name="lastUsrKey" type="java.lang.Integer" db-attribute-path="LAST_USR_KEY"/>
                    <obj-attribute name="psswd" type="java.lang.String" db-attribute-path="PSSWD"/>
                    <obj-attribute name="securityMstrKey" type="java.lang.Integer" db-attribute-path="SECURITY_MSTR_KEY"/>
                    <obj-attribute name="usrId" type="java.lang.String" db-attribute-path="USR_ID"/>
                    <obj-attribute name="usrKey" type="java.lang.Integer" db-attribute-path="USR_KEY"/>
            </obj-entity>
            
            
            
    </data-map>

    In jsp page

    <logic:present name="DisplayResult" scope="session">
                    <TR>
                            <TH>User ID</TH>
                            <TH>Name</TH>
                            <TH>Master Security Role</TH>
                            <TH>Active</TH>
                            <TH>Last Login Date</TH>
                            <TH>Action</TH>
                    </TR>
                    <logic:iterate id="record" name="DisplayResult" indexId="index">
                    <TR <% if (index.intValue() % 2 == 0) { %> class="ALTCOLOR" <% } %>>
                            <TD><bean:write name="record" property="usrId" /></TD>
                            <TD><bean:write name="record" property="lastName" />, <bean:write name="record" property="firstName" /></TD>
                            <TD><bean:write name="record" property="securityMstrKey"/></TD>
                            <TD align="center"><bean:write name="record" property="actvFlag" /></TD>
                            <TD align="center"><bean:write name="record" property="lastLoginDate" /></TD>
                            <TD align="center">
                                    <html:link action="EditUser" target="frmRight" name="record" property="adminUserIdParamValueMap">Edit</html:link>
                            </TD>
                    <TR>
                            
                    </TR>
                    </logic:iterate>
                    
                    </logic:present>

    Thanks
    sami

    CONFIDENTIALITY NOTICE. This e-mail and attachments, if any, may contain confidential information which is privileged and protected from disclosure by Federal and State
    confidentiality laws rules and regulations. This e-mail and attachments, if any, are intended for the designated addressee only. If you are not the designated addressee, you
    are hereby notified that any disclosure, copying, or distribution of this e-mail and its attachments, if any, may be unlawful and may subject you to legal consequences. If you
    have received this e-mail and attachments in error, please contact Independent Health immediately at (716) 631-3001 and delete the e-mail and its attachments from your
    computer. Thank you for your attention

    >>> dhrutiraman..ahoo.com 05/06/05 02:17PM >>>
    I tried the second option and it works now.
     
    Thank you so much. I really like this user group.
    Denna

    Andrus Adamchik <andru..bjectstyle.org> wrote:

    > I understand those concepts now. thanks.
    >
    > I tried the way you have suggested but still returns null values. Even
    > if I am not using complex query just "select * from cahflow", it returns
    > all rows with null value.
    >
    > Any idea?
    > Denna

    Doing "select * ..." may not produce the result that Cayenne understands.
    The outcome is DB and configuration-dependent. I suggest describing each
    column in the result explicitly buy using the #result() directive (see
    docs on it at
    http://objectstyle.org/cayenne/userguide/fetch/sqltemplate-scripting.html).

    Or use data rows [query.setFetchingDataRows(true) or corresponding
    checkbox in the Modeler] - Cris mentioned this briefly. The result of your
    original aggregate query won't map to any of the entities anyway, so data
    rows approach seems more sensible.

    What this means is that the result will contain java.utl.Map instances
    instead of DataObjects. Then you can take each such map and convert it to
    your "aggregated" object:

    SQLTemplate query = ...
    query.setFetchingDataRows(true);
    List rows = context.performQuery(query);

    Iterator it = rows.iterator();
    while(it.hasNext()) {
    Map map = (Map) it.next();
    // do whatever...
    }

    Andrus

                    
    ---------------------------------
    Do you Yahoo!?
     Make Yahoo! your home page



    This archive was generated by hypermail 2.0.0 : Fri May 06 2005 - 15:19:54 EDT