Advantages and disadvantages of using XML as a configuration file (for example, hibernate)

Source: Internet
Author: User
In this article we'll discuss the pros and cons of Hbm.xml and annotations, and see what's best for you.

First, the advantages of the XML configuration file are discussed, and the main advantage is that you don't need to change and recompile the code when you change the underlying configuration, just change it in the XML, for example Hibernate.cfg.xml when you want to change the underlying database, just change the configuration file. Hibernate will do something else for you.

So what is the downside of XML? Personally think there are several: descriptor, not easy to remember, master to understand and see the DTD file can not do automatic verification, the need to manually find read and parse XML configuration to consume a certain amount of time, resulting in application startup slow, not easy to test and maintenance when the system is very large, A large number of XML files are difficult to manage run save XML configuration need to consume extra memory in O/R mapping need to alternate between Java file and XML configuration file, increase the workload

The 12th of them may not be a problem with the help of an advanced IDE. But it's still a problem for beginners.

Let's look at the characteristics of annotations! You can solve the problems encountered by XML and have the following advantages to reduce the descriptor. Previously in XML configuration, it was often necessary to describe the types, relationships, and so on of Java properties. The metadata itself is the Java language, which omits a large number of descriptor compile-time checksums. Incorrect annotations will error during compilation. Metadata annotations in Java code, avoid additional file maintenance work metadata is compiled into Java bytecode, consumes less memory, reads quickly, and facilitates testing and maintenance

About mapping files using hbm.xml files or using annotations Let's take a look at the performance of 2. First of all, it is stated that the mapping file will not be changed to a large extent once it has been configured. So using an XML file does not have great benefits. If you think The mapping file also changes frequently in your project, such as a column of string data, today you use Length= "16" Tomorrow you think that the length of the data should be longer to meet the business needs and then length= "128" and so on similar problems. If you often have this change, the following comparison you can not see, you should use the XML file because annotations not very good to meet your requirements. When  session cleans up the cache, the database is updated based on the properties of the persisted object. The Gengxin,deng object is removed from the session's cache only when the close () method of the call is invoked.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.