Re: [JIRA] Commented: (CAY-811) Meaningful identity columns: user provided PK values are ignored

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Thu Aug 02 2007 - 14:41:58 EDT

  • Next message: Kevin Menard: "RE: PostgreSQL sequences revisited"

    Finally had a chance to look at the code (instead of suggesting
    random things :-)). I think a patch like that would work (in fact I
    tested it with POJO itests and it seems to do the right thing. If
    there are no objections I can commit it tomorrow.

    Andrus

    Index: framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/
    cayenne/access/DataDomainInsertBucket.java
    ===================================================================
    --- framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/
    cayenne/access/DataDomainInsertBucket.java (revision 560823)
    +++ framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/
    cayenne/access/DataDomainInsertBucket.java (working copy)
    ..-144,8 +144,15 @@
                                  .readPropertyDirectly(object);
                          if (value != null) {
    - // treat numeric zero values as nulls
    requiring generation
    - if (!(value instanceof Number && ((Number)
    value).intValue() == 0)) {
    + Class javaClass = objAttr.getJavaClass();
    + if (javaClass.isPrimitive()
    + && value instanceof Number
    + && ((Number) value).intValue() == 0) {
    + // primitive 0 has to be treated as
    NULL, or otherwise we
    + // can't generate PK for POJO's
    + }
    + else {
    +
                                  idMap.put(dbAttrName, value);
                                  continue;
                              }

    On Aug 2, 2007, at 6:42 PM, Andrus Adamchik wrote:

    > I see... The problem is that 'isGenerated' only applies to 'Db
    > Generated' PK strategy and does not apply to 'Default'. So maybe
    > leave the zero rule for primitives, and check the ObjAttribute
    > (don't know if it is easily available in that place without looking
    > at the code?) for whether the value is mapped as numeric primitive
    > or numeric object?
    >
    > Andrus
    >
    >
    > On Aug 2, 2007, at 6:35 PM, Tore Halset wrote:
    >> On Aug 2, 2007, at 14:59 , Andrus Adamchik wrote:
    >>
    >>> On Aug 2, 2007, at 3:52 PM, Tore Halset wrote:
    >>>
    >>>> On Aug 2, 2007, at 14:43 , Tore Halset wrote:
    >>>>
    >>>>> Handling null as 0 can probably lead to other problems as well.
    >>>>> Perhaps 'NullNumber extends Number' could be used? Or creating
    >>>>> a (empty) Null-interface and then subclass Integer, Long and so
    >>>>> on?
    >>>>
    >>>> The last one would not work as Integer is final.
    >>>>
    >>>> - Tore.
    >>>
    >>> I didn't quite understand the proposed solution (aside from the
    >>> "final" thing). What are we going to use NullNunber for? Could
    >>> maybe post some code examples?
    >>
    >>
    >> A better name would be UnsetPrimitiveNumber extending Number and
    >> return 0 for all the methods. That way it would be the almost same
    >> as new Integer(0), but could be tested for its type.
    >>
    >> I have digged a bit down in the POJO code and it looks like this
    >> approach will not work. Using reflection on a POJO, java will
    >> report the same for an unset int as an int set to 0. So (at least
    >> from a reflection point of view) it is the same.
    >>
    >> Could we use the DbAttribute.isGenerated flag to determine if the
    >> new Integer(0)-value should be handled? Attached is a patch that
    >> explain this variant. It looks like this variant passes all the
    >> junit tests and also fixes my problem.
    >>
    >> - Tore.
    >> Index: framework/cayenne-jdk1.4-unpublished/src/main/java/org/
    >> apache/cayenne/access/DataDomainInsertBucket.java
    >> ===================================================================
    >> --- framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/
    >> cayenne/access/DataDomainInsertBucket.java (revision 562134)
    >> +++ framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/
    >> cayenne/access/DataDomainInsertBucket.java (working copy)
    >>.. -144,8 +144,12 @@
    >> .readPropertyDirectly(object);
    >> if (value != null) {
    >> - // treat numeric zero values as nulls
    >> requiring generation
    >> - if (!(value instanceof Number &&
    >> ((Number) value).intValue() == 0)) {
    >> + // POJO/JPA with generated key mapped as
    >> a primitive type will
    >> + // have a Number with value 0 for a unset
    >> value
    >> + if (!(supportsGeneratedKeys
    >> + && dbAttr.isGenerated()
    >> + && (value instanceof Number) &&
    >> ((Number) value)
    >> + .intValue() == 0)) {
    >> idMap.put(dbAttrName, value);
    >> continue;
    >> }
    >>
    >>
    >>
    >
    >



    This archive was generated by hypermail 2.0.0 : Thu Aug 02 2007 - 14:42:25 EDT