Auto-generation of primary-key classes

The JDO spec defines a series of PK classes that will be used when you have a single primary-key field and don’t specify the objectidClass. This provides for the majority of situations meaning that the user can omit this time-consuming process of creating a PK class. Obviously this leaves the cases where you have more than 1 primary-key field. There are too many possible combinations of field types to be able to provide a series of PK classes as part of JDO, hence why this has always been an activity best handled by the user.

DataNucleus AccessPlatform 2.0 now has an option on the byte-code enhancer to auto-generate a primary key class when enhancing the class itself; it is turned on by default (you can turn it off using the option “generatePK”, setting it to false) and whenever the class has more than 1 PK field and has no objectidClass defined it will generate a PK class file. The generated PK follows all JDO rules on PK classes, implementing Serializable, having public fields with the same names as the class PK fields, String constructor that can process the String created by the toString() method, etc.

Note that this mode of the enhancer doesn’t generate the Java file, just the class file, meaning that you don’t have the overhead of having to manage the PK class once created – it will be created whenever you enhance the class in question.

Give it a try and see what you find. It’s been tested on simple cases only so far.

Advertisements
This entry was posted in JDO, JPA, primary-key. Bookmark the permalink.

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