Menu

Inserting an object with an associated object

Anonymous
2005-04-14
2013-03-07
  • Anonymous

    Anonymous - 2005-04-14

    Hi,

    I'm trying a simple test to create a new object and save it to my database. The object has a oneToOne association with another object, which I try to save at the same time -

    User newUser = new User();
    newUser.Name = "Some Name";

    AccountStatus ac = new AccountStatus();
    ac.Id = 1;
    ac.Retrieve();

    newUser.AccountStatus = ac;
    newUser.SetDirtyFlag();
    newUser.Save();

    (I probably don't need to do the retrieve as it only needs to associate with the ID, but I was testing that at the same time).

    The association looks like this -

    <association
            fromClass="User"
            toClass="AccountStatus"
            cardinality="oneToOne"
            target="AccountStatus"
            retrieveAutomatic="true"
            deleteAutomatic="false"
            saveAutomatic="true"
            inverse="false">
            <entry fromAttribute="AccountStatus" toAttribute="Id" />
        </association>

    However, when I try to save, I always get an error stating that no rows were changed as a result of the query. When I step through the code, I see that the command fails on it's attempt to insert because the SQL query it tries to execute does not include the AccountStatus_ID column.

    Looking through my map file, I see that I don't actually sepcify the name of the column in the Users table where it should insert the AccountStatus ID (I have the AccountStatus attribute, but it doesn't specify a column name (and specifying one doesn't seem to help)).

    I feel like I'm missing something very simple here.
    (I'm using the latest version from CVS).

    Thanks,
    Phil

     
    • Nobody/Anonymous

      You need to include a property in the class for the AccountStatus_ID.

      This property should then be mapped to the appropriate database column.

      Also the association should use the AccountStatus_ID attribute in the associations <entry /> node instead of AccountStatus (which I assume is an object).

       

Log in to post a comment.