Monday, January 16, 2006

@GeneratedValue annotation support in Glassfish

I reported previously that Glassfish does not yet support the new @GeneratedValue annotation specified in the EJB 3.0 PFD. I downloaded the latest build ( b33) of Glassfish today, and while testing my code, found that the new build supports @GeneratedValue. It is a pity that one has to find this out by trail and error; the Glassfish team should put up some release notes with each build which covers feature changes since last build.

Anyway, the old syntax for @Id annotation allowed atributes for specifying Id Generators. The new method requires the Id Generator to be specified separately using a @GeneratedValue annotation. Here is a comparison between the old approach and the new approach:

Old approach:
@Id (generate=GeneratorType.SEQUENCE, generator="SEQ_GEN") 
public Long id;
New approach:
public Long id;
I think the new approach is cleaner. It also enables Id Generator to be used in fields that are not necessarily the primary key.

Overall, the fact that Glassfish seems to be following the draft specification closely is great, as it enables developers to get familiar with the new technology. Thankfully, the specs are now close to final version, so changes ought to be relatively small.

Talking of changes, I hit another one. The syntax for @TableGenerator has changed. The table is not longer specified using @Table, instead, table and schema are ordinary string attributes. Here's an example of the new syntax:

@TableGenerator(name = "DISTRICT_ID_SEQUENCE", 
table = "IDGENERATOR", schema = "TPCC",
pkColumnName = "SEQ_NAME", valueColumnName = "SEQ_COUNT",
allocationSize = 10)

No comments: