You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by jerome moliere <je...@gmail.com> on 2007/03/07 16:50:35 UTC

Improvement ? code generation (templates)

Hi all, rather than using custom templates  could we imagine to change
current templates while adding
something like the Commons Lang
EqualsBuilder/HashCodeBuilder/ToStringBuilder in order to get self
explanatory
logs and quick sorts on Java Collections filled with Cayenne objects ? It
may also avoid some clever bugs in client code (not in Cayenne)..

If help required I may find some time to do that...

Regards
Jerome

-- 
Jerome Moliere - Mentor/J
http://romjethoughts.blogspot.com/
auteur Eyrolles

Re: Improvement ? code generation (templates)

Posted by Andrus Adamchik <an...@objectstyle.org>.
Ok, now I understand.

Let's take a look at it from Cayenne standpoint. Within a single  
DataContext Cayenne has a notion of uniquing, so no two objects are  
equal. Across the contexts, objects are equal if their ObjectIds are  
equal. Otherwise property value equality means nothing to Cayenne. So  
what's equal and what's not is very much context dependent :-) and  
therefore java.lang.Object.equals() is the most appropriate default.

Now, IIRC, there won't be much harm done if you decide to implement  
those methods on your own. But you don't have to put these methods in  
the generated code. A generic method placed in a common superclass  
can handle it easily (common superclass, aka "wedge class", is a  
subclass of CayenneDataObject and can be set via a "Superclass" field  
in the ObjEntity editor panel).

Cayenne persistent objects have access to their ObjectContext, so by  
definition they have access to their mapping metadata via  
EntityResolver, so comparison can be done in a generic fashion,  
iterating via mapped attributes.

Andrus



On Mar 7, 2007, at 6:30 PM, jerome moliere wrote:

> 2007/3/7, Andrus Adamchik <an...@objectstyle.org>:
>>
>> Hi Jerome,
>>
>> Could you give specific examples of the code you want to see
>> generated?
>
>
>
> yes of course I can, this is an excerpt from one of my Java class...
> This class has different attributes (name and so on):
>
>
>    /* (non-Javadoc)
>     * @see java.lang.Object#equals(java.lang.Object)
>     */
>    @Override
>    public boolean equals(Object obj) {
>        if (this == obj) {
>            return true;
>        }
>        if (!(obj instanceof PersistentField)) {
>            return false;
>        }
>        PersistentField rhs = (PersistentField) obj;
>        return new EqualsBuilder().append(name, rhs.name).append(
>                ordinalPosition, rhs.ordinalPosition)
>                .isEquals();
>
>    }// equals()
>
>    /* (non-Javadoc)
>     * @see java.lang.Object#hashCode()
>     */
>    @Override
>    public int hashCode() {
>        return new HashCodeBuilder().append(name).append 
> (ordinalPosition)
>        .append(genre).toHashCode();
>    }
>    /* (non-Javadoc)
>     * @see java.lang.Object#toString()
>     */
>    @Override
>    public String toString() {
>        return new ToStringBuilder(this).append("column", name).append(
>                "dataType", dataType).append("isNullable",
> isNullable).append("Fk member :",isFkMember).append("isPkmember",
> isPkMember())
>                .append("typname",genre).append("refers to  
> table:",refersTo)
>                .toString();
>    }
>
>
> these methods use the COmmons Lang main classes (ToStringBuilder  
> and so on)
> to generate the hashcode/equals/toString methods...
>
>> From your message I don't quite understand why we need to
>> change the *default* template?
> Because I think it may be generated directly from the Modeler as  
> the super
> classes (generated as _MyClass.java)
>
>
> Cheers
> jerome
> -- 
> Jerome Moliere - Mentor/J
> http://romjethoughts.blogspot.com/
> auteur Eyrolles


Re: Improvement ? code generation (templates)

Posted by jerome moliere <je...@gmail.com>.
2007/3/7, Andrus Adamchik <an...@objectstyle.org>:
>
> Hi Jerome,
>
> Could you give specific examples of the code you want to see
> generated?



yes of course I can, this is an excerpt from one of my Java class...
This class has different attributes (name and so on):


    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof PersistentField)) {
            return false;
        }
        PersistentField rhs = (PersistentField) obj;
        return new EqualsBuilder().append(name, rhs.name).append(
                ordinalPosition, rhs.ordinalPosition)
                .isEquals();

    }// equals()

    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(name).append(ordinalPosition)
        .append(genre).toHashCode();
    }
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return new ToStringBuilder(this).append("column", name).append(
                "dataType", dataType).append("isNullable",
isNullable).append("Fk member :",isFkMember).append("isPkmember",
isPkMember())
                .append("typname",genre).append("refers to table:",refersTo)
                .toString();
    }


these methods use the COmmons Lang main classes (ToStringBuilder and so on)
to generate the hashcode/equals/toString methods...

>>From your message I don't quite understand why we need to
>change the *default* template?
Because I think it may be generated directly from the Modeler as the super
classes (generated as _MyClass.java)


Cheers
jerome
-- 
Jerome Moliere - Mentor/J
http://romjethoughts.blogspot.com/
auteur Eyrolles

Re: Improvement ? code generation (templates)

Posted by Andrus Adamchik <an...@objectstyle.org>.
Hi Jerome,

Could you give specific examples of the code you want to see  
generated? From your message I don't quite understand why we need to  
change the *default* template?

Cheers,
Andrus

On Mar 7, 2007, at 5:50 PM, jerome moliere wrote:

> Hi all, rather than using custom templates  could we imagine to change
> current templates while adding
> something like the Commons Lang
> EqualsBuilder/HashCodeBuilder/ToStringBuilder in order to get self
> explanatory
> logs and quick sorts on Java Collections filled with Cayenne  
> objects ? It
> may also avoid some clever bugs in client code (not in Cayenne)..
>
> If help required I may find some time to do that...
>
> Regards
> Jerome
>
> -- 
> Jerome Moliere - Mentor/J
> http://romjethoughts.blogspot.com/
> auteur Eyrolles


Re: Improvement ? code generation (templates)

Posted by Juergen Saar <ju...@jsaar.org>.
I can't see why you don't implement your own template.

I use my own template since starting my project, because I found it useful
to have some additional getters and setters ...

Some central functionality we have implemented in our DataObject-Class
derived from CayenneDataObject. Perhaps this would  be helpful for you too.

--- Juergen ---

2007/3/7, jerome moliere <je...@gmail.com>:
>
> Hi all, rather than using custom templates  could we imagine to change
> current templates while adding
> something like the Commons Lang
> EqualsBuilder/HashCodeBuilder/ToStringBuilder in order to get self
> explanatory
> logs and quick sorts on Java Collections filled with Cayenne objects ? It
> may also avoid some clever bugs in client code (not in Cayenne)..
>
> If help required I may find some time to do that...
>
> Regards
> Jerome
>
> --
> Jerome Moliere - Mentor/J
> http://romjethoughts.blogspot.com/
> auteur Eyrolles
>