Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a good reason to configure hibernate with XML rather than via annotations?

I've been using Hibernate for a few years but have only used it with annotations, and by setting the connection parameters in my code.

Am I "missing something" by not using the XML files? Are there important capabilities available only in XML? Are there situations or patterns where it makes sense to use the XML?

like image 721
Uri Avatar asked Jan 17 '09 02:01

Uri


People also ask

Which annotation does not allow Hibernate to update the contents?

Use updatable=false in Hibernate to avoid updating the column.

Which of the following Hibernate XML elements can be used to override a Hibernate annotation based configuration?

One way to do this is using JPA XML configuration to override the existing class annotations.

Which annotation in JPA is not a mandatory annotation at the time of defining the entity class?

@Column. Let's start with the @Column annotation. It is an optional annotation that enables you to customize the mapping between the entity attribute and the database column.

Is an annotated class in hibernate Which of the following annotations are mandatory to persist an object?

@Table annotation specifies the table name where data of this entity is to be persisted. If you don't use @Table annotation, hibernate will use the class name as the table name by default. @Id annotation marks the identifier for this entity.


2 Answers

I think it's pretty safe to say that you're not missing out on anything.

If there are any capabilities in XML that can't be represented in attributes (and I believe there are some rare cases) then you still have the option to use [RawXml] and write the XML in the attribute. So you can't possibly miss out on any functionality.

It might make sense to use XML if you have enough programmers in your team who simply prefer to manage separate files or if there is a genuine need to edit xml mappings on the fly. Xml mapping files are probably easier to manipulate for very complex mapping and they can contain useful information (comments on fetching strategies etc).

There is also the issue of architecture, where some people argue that separating the mapping into XML files provides a better separation between business-related code and instructions on how it is persisted.

like image 130
PandaWood Avatar answered Oct 18 '22 21:10

PandaWood


Annotations are a great way to quickly configure Hibernate. But one of the key features of OR mappers like Hibernate is that you keep your domain model "persistence ignorant" your objects don't need to know anything about where and how they are persisted. Some may argue that using annotations breaks that. And in situations where persistence is just one of many concerns it makes sense to keep things separate.

Another reason can be that domain objects are persisted differently in different situations, you can have multiple databases used by one application or even more applications that use the same domain model.

like image 36
Mendelt Avatar answered Oct 18 '22 21:10

Mendelt