Menu

Problem with find

2004-11-07
2013-03-07
  • André Ribeiro

    André Ribeiro - 2004-11-07

    Hi,

    My database have three tables: Model, kind and fuel. It has associations  as following:
    Model - Kind (many-to-many)
    Model - fuel  (many-to-many)

    I have three class:
    -CKind
        - codKind
        - descKind
    - CFuel
        - codFuel
        - descFuel
    - CModel
        - codModel
        - Kind as Ckind
        - Fuel as CFuel

    I did the necessary changes at the XML with associations and with Sql Server DBMS.

    When I call the "find" method occurs an error and doesn't retrieve the attributes Kind and Fuel at CModel. Can you help me?

    Thank you,
    Andr Ribeiro from Brazil.

     
    • André Ribeiro

      André Ribeiro - 2004-11-08

      Hi,

      At this XML I think there is no errors. My XML is this. The find method doesn't populate the attribute Fabricante neither Tipo_Veiculo:
      <?xml version="1.0" encoding="utf-8" ?>
      <map>
          <database name="Transporte" class="CMsSqlDatabase">
              <parameter name="name" value="Transporte" />
              <parameter name="serverName" value="S2200DESV" />
              <parameter name="user" value="transporte" />
              <parameter name="password" value="xxx" />
              <parameter name="OIDTable" value="OID"/>
          </database>
          <class name="CTipoVeiculo" table="Tipo_Veiculo" database="Transporte">
              <attribute name="codigo" column="Tive_cd_Tipo" key="primary"/>
              <attribute name="descricao" column="Tive_nm_tipo" find="true" proxy="true"/>
          </class>
          <class name="CFabricante" table="Fabricante" database="Transporte">
              <attribute name="codigo" column="Fabr_cd_Fabricante" key="primary"/>
              <attribute name="nome" column="Fabr_nm_Fabricante" find="true" proxy="true"/>
          </class>
          <class name="CModelo" table="Modelo" database="Transporte">
              <attribute name="codigo" column="Mode_cd_modelo" key="primary"/>
              <attribute name="nome" column="mode_nm_modelo" find="true" proxy="true"/>
              <attribute name="Fabricante"/>
              <attribute name="codigoFabricante" column="Fabr_cd_Fabricante" proxy="true"/>
              <attribute name="Tipo_Veiculo"/>
              <attribute name="codigoTipoVeiculo" column="Tive_cd_tipo"/>
          </class>
         
          <association fromClass="CModelo"
              toClass="CTipoVeiculo"
              cardinality="oneToMany"
              target="Tipo_Veiculo"
              retrieveAutomatic="true"
              deleteAutomatic="false"
              saveAutomatic="true"
              inverse="false">
              <entry fromAttribute="codigoTipoVeiculo" toAttribute="codigo"/>
          </association>
         
          <association fromClass="CModelo"
              toClass="CFabricante"
              cardinality="oneToMany"
              target="Fabricante"
              retrieveAutomatic="true"
              deleteAutomatic="false"
              saveAutomatic="true"
              inverse="false">
              <entry fromAttribute="codigoFabricante" toAttribute="codigo"/>
          </association>
      </map>

       
    • Richard Banks

      Richard Banks - 2004-11-09

      There is a small potential problem with your associations.

      Your parent class has a property codigoFabricante which can hold a key value for a single CFabricante object.

      You have defined the association as a one to many association, but the mappings look like you really want a one-to-one association to me.

      If you want a one to many, you should have a codigoModelo attribute in your CFabricante class that is linked back to the codigo attribute of the CModelo class.

      You should see that (at the moment) the SQL generated is trying to link from CFabricante to CModelo where Fabricante.Fabr_cd_Fabricante = Modelo.Fabr_cd_Fabricante.  At most this will return 1 record (run the generated SQL in QueryAnalyzer to see what is actually returned).

      Does that make sense to you?

      - Richard

       
      • André Ribeiro

        André Ribeiro - 2004-11-09

        Oh!! That's ok. I was thinking about the relations of the relational database... One to many... Now that's ok. I will try again!

        Bye,
        Andr Ribeiro

         
    • André Ribeiro

      André Ribeiro - 2004-11-11

      Hi,

      I have the follow XML:
      <class name="CModelo" table="Modelo" database="Transporte">
              <attribute name="codigo" column="Mode_cd_modelo" key="primary"/>
              <attribute name="nome" column="mode_nm_modelo" find="true" proxy="true"/>
              <attribute name="Fabricante"/>
              <attribute name="codigoFabricante" column="Fabr_cd_Fabricante" find="true" proxy="true"/>
              <attribute name="Tipo_Veiculo"/>
              <attribute name="codigoTipoVeiculo" column="Tive_cd_tipo"/>
          </class>"

      Two attributs has the find="true" property. If I pass an object CModelo only with the Fabr_cd_Fabricante the framwork will create the follow sql clause: "where mode_nm_modelo IS NULL and Fabr_cd_Fabricante = 1" OK?

      If I want to create the clause "where
      Fabr_cd_Fabricante = 1" I must instance a CRetrieveCriteria and mount my SQL dinamically?

      Thak you,
      Andr Ribeiro from Brazil

       
      • Richard Banks

        Richard Banks - 2004-11-11

        When you execute the find() method the framework will attempt to retrieve an object using all of the attributes marked with find="true".  This is why your where clause uses both mode_nm_modelo and fabr_cd_fabricante.

        The find attributes are designed to be used as a secondary unique key to the class (specifically for cases where you use Guids or OIDs to abstract primary key information).

        If you just want fabr_cd_fabricante then you will need to use a CRetrieveCriteria as you have said.  To make programming a little easier you could create a method in the CModelo class that can be used anywhere.

        - Richard.

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.