What is the most preferable way to specify the object relational mapping when using EJB 3 entity beans? Is it using an XML persistence unit or annotation in each entity? What are the advantages and disadvantages? (I have tried out xml for this but I guess annotations also can be used, can't they?)
much of the annotation discussion boils down to personal preferences. Annotations can certainly help you to get up an running quickly but you might find XML less cumbersome when getting closer to production/operations.
Personally, I use annotations only for things that are really close to the code, e.g. marking an annotation as one-to-many. I prefer to use XML to define the mapping itself, e.g. specifying table or column names etc.
The reason for that is that these mapping decisions are somewhat independent of the code and should be able to change (within reason) without having to change the code.
Much of this reasoning holds for annotations related to other technologies, too.
With Annotations, you can always overwrite them with xml, so in the cases where you object model might be reused in different applications, you can have your default mapping in Annotations, and overwrite with xml for the other applications that need something other than the default.
But it is personal preference. And there are no right or wrong answers.
Originally posted by Gregg Bolinger:
+1 on using Annotations. I just recently started glancing at hibernate again and running through some examples and I find annotations much simpler and easier to manage. Interestingly though, I prefer using XML for wiring up beans in Spring over the annotations available now in Spring 2.1.
Wait a second, should I alert the media. I think Gregg finally agrees with me on a subject.
I've used xdoclet with EJB 2.1 which I think is similar to annotations and have used Spring and Hibernate which I think is similar to XML. I think I would rather use annotations if I am developing a system from the scratch (without having to use an existing table structure).
Thanks for your advice guys.
I tend to work with reasonably large domain object models (dozens of persistent classes; and I tend to work with JDO, but that's a different story...) When initially writing the code I'm often happy to use the default mapping provided by the persistence provider. As I progress, I start to influence the mapping manually, e.g. deciding on an inheritance mapping strategy, table and column names, details on column types and sizes.
On many projects a database specialist will have a say in the design of the database schema. If you have the luxury of working with an open-minded database specialist, this specialist may be willing to make required changes to the mapping information directly--this is feasible when you define these in XML, but less so if you define these in annotations.
(I understand that I can use XML to override the annotation settings, but I would advise against mixing XML and annotations for a specific piece of metadata.)
Also, my mileage with respect to the coupling of Java code to database mapping varies from Mark's; I find I can change my database schema quite a bit without having to change code.