Re: Key generator question

From: Fabricio Voznika (fabricio.voznik..artmouth.EDU)
Date: Tue Mar 04 2003 - 16:56:21 EST

  • Next message: Dave Paules: "RE: Runtime Error: how to find cayenne.xml"

    Hi 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.

    Andriy Shapochka wrote:

    >Ops, I missed this list somehow.
    >This feature is not integrated into our modeler. Still, added to a data map
    >manually, the xml generator configuration will not be lost in case you open,
    >modify, and save the data map in the Modeler later.Otherwise it is fully
    >supported and works in Cayenne. In my own project I am working with custom
    >sequences only. Here is an extract from one of the field data maps:
    >
    ><db-entity name="REQ_GRAPH">
    > <db-attribute name="ELEMENT_ID" type="INTEGER" isMandatory="true"/>
    > <db-attribute name="REQ_GRAPH_ID" type="INTEGER" isPrimaryKey="true"
    >isMandatory="true"/>
    > <db-key-generator>
    > <db-generator-type>ORACLE</db-generator-type>
    > <db-generator-name>REQ_GRAPH_SEQ</db-generator-name>
    > <db-key-cache-size>5</db-key-cache-size>
    > </db-key-generator>
    > </db-entity>
    >
    >To see how the job is done please have a look at
    >OraclePkGenerator.pkFromDatabase(DataNode node, DbEntity ent);
    >The code in it gives preference to a db-key-generator configured in the
    >DbEntity and in case the generator has not been found falls back to the
    >Cayenne classic pk generation method. To configure your sequences please
    >follow the guidelines in data-map.dtd
    >
    >Andriy.
    >
    >----- Original Message -----
    >From: "Andrus Adamchik" <andru..bjectstyle.org>
    >To: <fabricio.voznik..artmouth.EDU>; <ashapochka@hotmail.com>
    >Cc: <cayenne-use..bjectstyle.org>
    >Sent: Tuesday, March 04, 2003 10:20 PM
    >Subject: Re: Key generator question
    >
    >
    >
    >
    >>One of our committers, Andriy, implemented that feature recently. It may
    >>not be fully integrated yet and is probably not supported by the modeler.
    >>Andriy, can you comment on that?
    >>
    >>Andrus
    >>
    >>P.S. Andriy, you might want to subscribe to to cayenne-user, your recent
    >>additions are in really high demand ;-)
    >>
    >>
    >>
    >>
    >>>Hi,
    >>>
    >>> Does anyone know if it's possible to use a custom oracle sequence to
    >>>generate PKs? The following lines were extracted from data-map.dtd:
    >>>
    >>>
    >>>
    >
    >
    >



    This archive was generated by hypermail 2.0.0 : Tue Mar 04 2003 - 17:01:13 EST