Frank and I collected some Hibernate tips, and we named them with some sexy names ;-) This afternoon I tried to replace hibernate by JSR220 persistent API in a typical web application, and I found some new tips in JSR220 Persisntent API (Frank, I'm here waiting your names).

btw: I'm a believer in Peter Coad's Color Modeling approach, all the following tips are modeled in the approach. I recommend you the greate book 'Java Modeling in Color with UML' if you'd no idea about the Color UML.

1. Annotating Description with @Embeddable, and the Thing it describe with @Entity

Take Product and ProductDescription for example.

 2class ProductDescription {
 3 private String name;
 4 private String description;
 5  . and getters and setters

 8@Entity(access = AccessType.FIELD)
 9class Product {
11 private ProdcutDescription description;


In hiberante, we could make the ProductDescription as an embedded persistent object of Product via the component tag.If the ProductDescription should be embedded in another object, we've to declare it once more.
Things become more convenient in JSR 220, because non-transite fields, whose class is annoated with @Embeddable, are treated as embedded persistent objects automatically, we'd have fine-grained persistent objects without long-winded config file.

2.Avoiding Database Field in Domain Model Using @AttributeOverride

It's claimed that Annotaton could simplify development, but considering the cost of hard-coding mapping information in source codes I prefer some other complicated ways.It's too ugly to write code like following.

1@Entity(access = AccessType.FIELD)
3class Product {
5   @Basic
6   @Column(name="NAME", nullable=false, length=512)
7   private String name;

It's sucks but could be avoided.

 2class Product {
 4  @Basic
 5  private String name;

 9@AttributeOverride(name="name", column=@Column(name="Name"))
11class PersistentProduct extends Product {

We could separate the domain model and the persistent model by inheritance.Although we could do the same thing in hiberante too, we have to provide lots of configuration files.Once more we'd have hierachical persistent objects without long-winded config file.

3.Avoiding Database Primary Key in Domain Model

In most O/R mapping framework, we are always asked to give a primary key or identifier to our domain model, and we are suggested using meanless identifier instead of domain-specified identifer.It's kinda of Domain Pollution.Although we couldmake the domain model more clearer via inheritance, it's pointless for common usages.
Fortunately, we HAVE TO separate domain model and persistent model(as above mentioned), so that we can throw this bad habit away conviniently :D

2class Product {

6class PersistentProduct extends Product{
8   @Id private long primaryKey;


After two hours walking around JSR220 persistent API, I figured out that JSR220 mixed ugly into convenience... and it's quite easy to write ugly codes in that we've to insist on using OO and the convenience jsr220 provided to make our domain model as clear as possible.
Although JSR220 is more ugly than hibernate, we could get clearer domain model in jsr220 rather than hibernate, because we could not stand the unly in jsr220...weird way to make us keeping more isn't it...