London Bombings Howards Right: public abstract class Enum<E extends Enum<E>>
Jul 08

Hibernate3 Example of Annotation Hell :)

Java, Tech Add comments

I am getting to play with Hibernate 3, and they have done a nice job. I was poking around the Caveat Emptor example from Hibernate in Action, which was updated for Hibernate3.

I happened across this file: package-info.java:

@TypeDefs({
@TypeDef(
name="monetary_amount_usd",
typeClass = MonetaryAmountType.class,
parameters = { @Parameter(name="convertTo", value="USD") }
),
@TypeDef(
name="monetary_amount_eur",
typeClass = MonetaryAmountType.class,
parameters = { @Parameter(name="convertTo", value="EUR") }
),
@TypeDef(
name="item_state",
typeClass = EnumUserType.class,
parameters = { @Parameter(name="enumClassName", value="org.hibernate.ce.auction.model.ItemState") }
),

@TypeDef(
name="creditcard_type",
typeClass = EnumUserType.class,
parameters = { @Parameter(name="enumClassName", value="org.hibernate.ce.auction.model.CreditCardType") }
),
@TypeDef(
name="rating",
typeClass = EnumUserType.class,
parameters = { @Parameter(name="enumClassName", value="org.hibernate.ce.auction.model.Rating") }
)
})

@NamedQueries({
@NamedQuery(
name="minBid",
queryString="select b from Bid b where b.amount.value = (select min(b.amount.value) from Bid b where b.item.id = :itemid)"
),
@NamedQuery(
name="maxBid",
queryString="select b from Bid b where b.amount.value = (select max(b.amount.value) from Bid b where b.item.id = :itemid)"
)
})

package org.hibernate.ce.auction.persistence;

import org.hibernate.annotations.*;
import javax.persistence.*;

Is this better than XML? To have this code sucked in?

NOTE: You don’t have to use the annotations of course.

9 Responses to “Hibernate3 Example of Annotation Hell :)”

  1. coderonin Says:

    But dude, didn’t you read! The example is called Caveat Emptor, so watch out before you buy into the whole annotation thing! ;-)

  2. Rob Says:

    What a mess…

  3. Noone Says:

    Hibernate works MUCH better with Spring. You should wrap Spring around it. :)

  4. levmatta Says:

    NO it is no better for THIS CASE.
    But annotations are better for defining entity beans. Personally I am im doubt with type definitions, but they are a lot worse for defining querys for sure (because you do not have the ability to change this querys in the future).

  5. Gavin Says:

    Um, I’m finding it kinda hard to see what’s so bad about this.

    Since you didn’t show the equivalent XML next to it, its pretty hard to judge which is better, isn’t it?

    Note that this metadata is NOT occurring in a class definition, but as package-level metadata. So if you prefer, you can put it in XML even if you are using annotations in your classes.

    But, from what I can see, a bunch of typedefs and named queries isn’t especially prettier in XML. You’re just more used to seeing XML ugliness than annotation ugliness.

    What we are finding is that people who are using annotations in *practice*, rather than as tourists, absolutely love it. So many people came up to me at JavaOne and told me this….

  6. Dion Says:

    Note: I am not trying to say “annotations are bad” here at all.

    I think annotations are good in general.

    I think there are great uses of annotations.

    I think there are good uses of annotations wrt persistence.

    I just worry when I see huge sets of annotations like this.

    Dion

  7. Adam Kruszewski Says:

    Imho using annotations for NamedQueries is one of the dumbest thing that can be done ™. What if your DB is changed to another, different one? You just end rebuilding the whole thing.
    But the typedefs can be set up with annotations, that way when you rename the class it will be renamed also, assuming you are using smart IDE. (but of course you probably wouldn’t rename a class the first place ;-))

  8. Val Says:

    I warned about what I called the “annotation hell syndrom” more than a year ago arguing that in order to be useful annotations had to be used sparely. Read more at:

    The annotation hell syndrom:
    http://radio.javaranch.com/val/2004/05/14/1084533540000.html

    The annotation hell syndrom: Part 2
    http://radio.javaranch.com/val/2004/07/23/1090568800000.html

    The Annotation Saga: Feedback to Cédric and Bill
    http://radio.javaranch.com/val/2004/07/26/1090824307000.html

  9. NeVaL Says:

    http://filmiki-najlepsze.lolas.pl ^^^ filmiki najlepsze ^^^ [url]http://filmiki-najlepsze.lololas.pl[/url]

Leave a Reply

Spam is a pain, I am sorry to have to do this to you, but can you answer the question below?

Q: Type in the word 'cricket'