Re: Key generator question

From: Andriy Shapochka (ashapochk..otmail.com)
Date: Wed Mar 05 2003 - 03:45:51 EST

  • Next message: Holger Hoffstätte: "Re: Runtime Error: how to find cayenne.xml"

    Hi Fabricio,

    I see, most likely, this feature was not included into alpha 6. To get access to it you should check out the code from CVS and build it like it is described in Cayenne Developer's Guide. As for your second question, when you define an ObjAttribute for the entity's primary key you indicate primary key values are meaningful to your application like SSNs or something. In this case Cayenne lets you handle them and does not generate primary key values. You can use DataObject.getObjectId() to access primary key values for autogenerated pks not attached to an ObjAttribute. See also section "Primary Key Generation" in Cayenne User Guide.

    Andriy.

          Thanks for your quick answer! The code I have here (1.0a6) doesn't seem to do that. Am I missing something?

          protected int pkFromDatabase(DataNode node, DbEntity ent)
              throws Exception {

              Connection con = node.getDataSource().getConnection();
              try {
                  Statement st = con.createStatement();
                  try {
                      ResultSet rs = st.executeQuery("SELECT "
                                  + sequenceName(ent)
                                  + ".nextval FROM DUAL");
                      try {
                          if (!rs.next()) {
                              throw new CayenneRuntimeException(
                                  "Error generating pk for DbEntity "
                                      + ent.getName());
                          }
                          return rs.getInt(1);
                      } finally {
                          rs.close();
                      }
                  } finally {
                      st.close();
                  }
              } finally {
                  con.close();
              }
          }

      BTW, the modeller has actually erased my code.

      I have just one more question if I may. Playing a little bit more with it, I've found out that if I have an ObjAttribute in the data-map.xml for the PK, the value is not auto-generated by Cayenne. If I remove it, Cayenne will call the method above, get the next value in the sequence and assign it. Is that how we flag that we want to use an auto-generated value, creating a Integer PK and not having an ObjAttribute for it?

      Thanks a lot,
      Fabricio.



    This archive was generated by hypermail 2.0.0 : Wed Mar 05 2003 - 03:49:09 EST