Oisín Hurley's Weblog

old dog, new tricks

Archive for September 2007

An Infestation of Snails (was: Java Annotation Madness)

with one comment

I was just reading an article on JPA, posted on TheServerSide. In it we see a simple ‘real-world’ example – a mocked-up blog application. The object model looked fine, the DB schema representation looked fine, but then I got to see the Java, annotated JPA-stylee.


import javax.persistence.*;

@MappedSuperclass
@EntityListeners({ModelListener.class})
public abstract class ModelBase {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    private Long id;

    @Version
    @Column(name="version")
    private Integer version;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="created_by_user_id")
    private User createdByUser;

    @Temporal(value = TemporalType.TIMESTAMP)
    @Column(name = "date_created", nullable = false)
    private Date dateCreated;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="updated_by_user_id")
    private User updatedByUser;

    @Temporal(value = TemporalType.TIMESTAMP)
    @Column(name = "date_updated", nullable = false)
    private Date dateUpdated;
    // methods removed for readability (LOL)
}

I got a bit of a chuckle out of the comment at the end of class declaration, but my overall feeling was one of giddiness at the sight of Annotations Gone Mad. It’s fashionable, it appears, to bang in as many annotations as possible in Java ‘frameworks’. If you have dealt with debugging C# webservices clients, you might recall the brain damage that was introduced when trying to fish through dozens of C# annotations in the generated code. Is this going to happen all over again, but for Java this time?

My particular complaint is not about using annotations, which I do think are wickedly useful for getting rid of the omnipresent and immiscible configuration APIs in thousands of products. Being able to inject values into class members is brilliant. There are even some gnarly uses where annotation declarations may themselves have annotations (go look at the Apache Camel source code). But using annotations to tie Java programs to what are irreducible and unmappable resources – ports, URLs, even DB tables – doesn’t make a lick of sense to me.

Update: Benson Margulies has given this condition a name – an infestation of snails (@) – in a cxf-dev posting. This is so good I had to change the name of this entry.

Advertisements

Written by oisinhurley

September 19, 2007 at 11:55 am

Posted in Coding, java, opinion