JPA 2, The Access annotation

JPA 1 has a strict rule about what the access when defining your meta annotations (you database to java mapping). In jpa 1 the standard was the way the ID was mapped. So when you placed your @Id or @EmbeddedId annotation (which is mandatory for every bean annotated with the @Entity annotation) on a field, all access will happen through the fields, when you apply it on the getter/setter methods, all fields will use the getter/setter methods.
JPA 2 adds some extra flexibility here. When you want your fields to be access through the fields, but there is one field that you want to modify when setting or getting, you can provide the @Access annotation.
This annotation defines the path of the persistence provider. It basicly has 2 modes

AccessType.FIELD, AccessType.PROPERTY;

You first have to annotate your Entity to set the base access mode, so the @Id rule is overruled.

@Entity
@Access(AccessType.FIELD)
public class Car implements Serializable {
// some fields, getters and setters
}

We have now configured the Pojo to use the fields unless we define it otherways.
by placing the @Access annotation on top of a getter (not setter, this will give an error that the annotation only should be applied on no-param methods), you tell it should use the getter/setter instead of direct access to the field.

@Access(AccessType.PROPERTY)
    public Brand getBrand() {
        return brand;
    }

    public void setBrand(Brand brand) {
        this.brand = brand;
    }

Although the usefulness of this feature is definitely arguable, I do want to share this. :-)

Java EE annotations in Spring

Since I began programming I noticed that annotations kept gaining popularity. Implementing a marker interface is in many cases replace by adding an Annotation.

Even in Spring. Spring added the support tor the java EE annotations. Here’s a little example. Instead of implementing the initializingBean Interface, you can simply use theĀ @PostConstruct van Java EE 5.

All callback handler used in EE5+ are possible in Spring. you only have to configure theĀ CommonAnnotationBeanPostProcessor in your spring context.

/** {@inheritDoc} */
public void afterPropertiesSet() throws Exception {
}

becomes

/** Postconstuction method */
@PostConstruct
public void myMethod() {
}

It is a small nuance, but I personally like this way of working. I don’t chose annotations over interfaces in every case, but when we are talking about callback handlers, I think it the better way. Your implementation doesn’t include strange spring specific methods, but methods that make sense to you and your other developers. It just has a little annotation above it.