1.3. Programming with Kodo

1.3.1. How long will it take me to learn the JDO APIs?
1.3.2. What standard APIs do I need to be familiar with to use Kodo?
1.3.3. What is the fastest way to get going with Kodo?
1.3.4. Do you provide any example applications using Kodo?
1.3.5. How do I issue queries to the database using Kodo?
1.3.6. How much more programming do I need to do to use Kodo?
1.3.7. What advantages does Kodo have over other persistence APIs?
1.3.8. Can Kodo we used in conjunction with other applications that operate against the same database?
1.3.9. Does Kodo provide extensions to the JDO API?
1.3.10. How do I avoid vendor lock-in when using Kodo?
1.3.11. Is the application that I write in Kodo portable to other JDO vendors?
1.3.12. How does JDO interact with the data access/transfer pattern (DAO / DTO)?
1.3.13. How does Kodo affect the build process of my application?
1.3.14. What is bytecode enhancement?
1.3.15. How can I debug enhanced persistent classes?
1.3.16. Can I use JDO without having to enhance my classes?
1.3.17. What is the differences between datastore caching and query caching?
1.3.18. How should I choose among the different ways to store my mapping information?
1.3.19. When using the mappingtool to create a schema for me, why does Kodo create a column called NAME0 for a field called name?
1.3.1.

How long will it take me to learn the JDO APIs?

The JDO API is designed to be extremely simple. You can view the entire JDO API at http://java.sun.com/products/jdo/javadocs.

1.3.2.

What standard APIs do I need to be familiar with to use Kodo?

Aside from the JDO APIs, you do not need to have any expertise in any APIs aside from the basic standard ones that the Java core library provides.

1.3.3.

What is the fastest way to get going with Kodo?

The Kodo tutorial provides a good introduction to developing a small application using the JDO APIs. See Chapter 2, Kodo JDO Tutorial.

1.3.4.

Do you provide any example applications using Kodo?

Kodo ships with numerous sample applications that can be adapted for your needs. For an overview of the samples that come with the Kodo distribution, see Chapter 1, Kodo Sample Code.

1.3.5.

How do I issue queries to the database using Kodo?

JDO specifies a query language called JDOQL, which allows queries to be written in a object-centric way. See Section 11.2, “JDOQL”. Kodo also offers the ability to directly execute SQL queries against the database. See Section 13.4, “Direct SQL Execution”.

1.3.6.

How much more programming do I need to do to use Kodo?

One of the goals of JDO's transparent persistence is to make the persistence code in your application as minimal and unintrusive as possible. Typically, you will only need to write in the transaction demarcation, object queries, and the addition of root objects to the database.

1.3.7.

What advantages does Kodo have over other persistence APIs?

Kodo is much less intrusive than other persistence APIs, in that your code does not need to be constantly "polluted" with additional code to do things like traversing relations. In addition, Kodo's bytecode enhancement allows performance advantages that cannot be matched by other reflection-based persistence architectures, such as declarative "fetch groups", automatic change detection, and and transparent relation traversal.

1.3.8.

Can Kodo we used in conjunction with other applications that operate against the same database?

Yes. Kodo does not require that it have exclusive read or write access to your database. The only restriction is that some optimistic lock strategies may need to be respected by other applications. For locking column considerations, see Section 7.7, “Version Indicator”.

1.3.9.

Does Kodo provide extensions to the JDO API?

As well as providing complete support for the core JDO API, Kodo does provide API extensions for some advanced or JDBC-specific operations. Many of Kodo's API extensions are under consideration for inclusion in the next major version of the JDO specification.

1.3.10.

How do I avoid vendor lock-in when using Kodo?

To avoid tying your application to any particular JDO vendor, you should avoid using any non-standard API extensions (i.e., avoid using any APIs that are not in the "javax.jdo" package). This will ensure that you can write your application in a way that will run in exactly the same way with any JDO compliant software.

1.3.11.

Is the application that I write in Kodo portable to other JDO vendors?

Yes, provided that the vendor's JDO implementation is truly compliant with the JDO specification. Since the JDO specification mandates that bytecode enhancement be done in a portable way, you have both source and binary portability of your JDO application.

1.3.12.

How does JDO interact with the data access/transfer pattern (DAO / DTO)?

While you can wrap JDO in the DAO pattern, most users find it easier to use JDO APIs directly. JDO provides much of DAO's functionality in a standardized and easy to use API set.

1.3.13.

How does Kodo affect the build process of my application?

The only change to your build process will typically be the addition of an "enhancement" phase.

1.3.14.

What is bytecode enhancement?

Bytecode enhancement is the mechanism by which JDO achieves persistence transparency. It involves running a tool on those classes that you have declared to be persistent. The class files will be changed internally to mediate access to all the fields that are marked as transparent. For more details, see Section 4.1, “JDO Enhancer”.

1.3.15.

How can I debug enhanced persistent classes?

Kodo conforms to the mandate in the JDO specification that enhanced classes retain their line number tables. This means that lines in stack traces will match those of your original Java source file. Furthermore, enhanced classes can be used by debuggers in exactly the same way as unenhanced classes.

1.3.16.

Can I use JDO without having to enhance my classes?

JDO does not require bytecode enhancement to function, although not using enhancement involve writing all your persistent classes to implement the javax.jdo.spi.PersistenceCapable interface. As well as removing some of the transparency of JDO, it makes the process considerably more complex. For an example of using JDO without enancement, see the noenhancement sample application (Section 1.9, “Using Persistent Classes Without Enhancement”).

1.3.17.

What is the differences between datastore caching and query caching?

Datastore caching caches data from the database. Basically, it caches your persistent objects. E.g. if you have a Person with id 100, and it's in the cache, if you try to reference Person with id 100, it won't have to go to the database to get the data.

The query cache, on the other hand, caches the results of a query. So, e.g., if you did a query for people whose "salary > 100" (and whatever other parameters, etc. that you want), and get back a Person- 100, Person-101, Person-102, then the next time you do that same query, you'll get back the same objects (unless you invalidate the query by changing objects, or manually).

So, an example of where these are different are if you did a query for "salary > 100" and then a query for "salary > 1000", the second query would be run against the database (it couldn't use the prior cached query), but all of the retrieved objects would be in the datastore cache, so the data for them wouldn't need to be retrieved.

1.3.18.

How should I choose among the different ways to store my mapping information?

There are four common places that you can store mapping information in Kodo. Choosing the right one for your application can be daunting. In general, we recommend that people stick with the default (store mapping information in a separate .mapping file), as it keeps the JDO metadata files and your source code clean. Further, we recommend putting all your mapping information for a given package into a package.mapping file, rather than using separate mapping files for each class.

However, other factors come into play as well. If you use a number of different databases at the same time, you may want to use the db mapping factory, so that you can easily have different mappings for different databases. If you like to have a single source document per class, then maybe XDoclet tags are the way to go. See Section 7.3, “Mapping Factory” for a detailed description of the different options available to you.

However, wherever you put them, remember that mapping information must be complete. That is, you cannot list partial information for just those fields whose column names you want to rename. This can mean that things can get a bit verbose when using XDoclet extensions.

1.3.19.

When using the mappingtool to create a schema for me, why does Kodo create a column called NAME0 for a field called name?

When Kodo automatically generates a column name for a field, it ensures that the name fits within the limitations of your database. This means that a long field name might be truncated, and that fields whose names are common SQL keywords (such as name) will have a 0 appended to the column name.

If the auto-generated column names bother you, you can always manually edit your mapping information to control exactly what your schema should look like.