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.
Now, I am on the other side of Oliver, I prefer Annotations. Because in probably about 90% of the time you need to change your mapping, you will also need to change the code to. I try to use XML as little as possible as I find it more difficult to maintain from the standpoint of having to look at both the Java class and the xml to figure things out, where with Annotations, I have to look at just one place.
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.
+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.
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 get your point(s). It's like if you use XML, you can see the entire mapping in one place. But with annotations, you have to look at each individual file. The experience I have with Spring and Hibernate is that even though having all the mappings in one place is good, it's a pain when a new developer comes and tries to modify something. You have to hunt through a number of XML files (most probably do a text search) to get to the place you want. But with annotations I think all the related info is in one place and it goes well with modularity principle of OO too. (Persistency of each class is its own business) So if you are thinking (and designing) in relational paradigm (may be working on a legacy system or some of your developers prefer to think in the terms of tables or you are considering the object model and the data model as two different things and you are really using JPA as an OR mapping tool) XML will be good. But if you are thinking in purely OO paradigm (you don't consider the object model and the data model as two different things but the data model as a derived model of the object model) then annotations are better because you only need to look at a class diagram and don't need to bother about the table structure.
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.
posted 13 years ago
I agree with Mark that there are no right or wrong answers. Nonetheless, I'd like to offer some additional thoughts:
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.