Oisín Hurley's Weblog

old dog, new tricks

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

One Response

Subscribe to comments with RSS.

  1. Yikes, this really reminds me of how much Java code sucks by not having frameworks that do decent ‘convention over configuration’ (COC)…

    John O'Shea

    September 21, 2007 at 9:08 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: