java Hibernate 映射文件和注解的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10010435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between Hibernate mapping file and annotation
提问by Lolly
Today I was trying to create a application using Hibernate as ORM. So while creating I had a doubt. What is the best practice to use, Hibernate mapping file (.hbm file) or annotations ? What are the pros and cons of it ? Please help me in understanding.
今天我试图创建一个使用 Hibernate 作为 ORM 的应用程序。所以在创作时我有一个疑问。使用的最佳实践是什么,Hibernate 映射文件(.hbm 文件)或注释?它的优缺点是什么?请帮助我理解。
回答by Bozho
- there is no functional difference. You can do (almost) the same things with both approaches
- xml files were used before Java had annotations (added in 1.5), so they can be considered an outdated way of mapping
- it is generally preferred to use JPA annotations rather than hibernate-specific ones; if using xml - there is a JPA xml format, which should be preferred to hibernate native one
- 没有功能差异。你可以用这两种方法做(几乎)同样的事情
- xml 文件是在 Java 有注解(在 1.5 中添加)之前使用的,因此它们可以被认为是一种过时的映射方式
- 通常首选使用 JPA 注释而不是特定于休眠的注释;如果使用 xml - 有一种 JPA xml 格式,应该优先于休眠原生格式
回答by Johanna
The question is what is your taste - both ways can do (mostly) the same, the difference is how to write.
问题是你的口味是什么 - 两种方式都可以(大部分)相同,区别在于如何写作。
With annotations you have the Java member variable/getter and the mapping directly together at one place.
通过注释,您可以将 Java 成员变量/getter 和映射直接放在一个地方。
Xml mapping files give a better overview over the table and its mapping.
Xml 映射文件可以更好地概述表及其映射。
In my opinion xml mapping files help for a better design of the database and application. Annonations tend to force the direction Java class -> mapping -> database table, which is the wrong direction (the database always should be designed first - changing database tables later is a lot of effort - most performance leaks are in a bad database design - Java classes easily can be changed any time).
在我看来,xml 映射文件有助于更好地设计数据库和应用程序。Annonations 倾向于强制方向 Java 类 -> 映射 -> 数据库表,这是错误的方向(总是应该首先设计数据库 - 稍后更改数据库表是很多工作 - 大多数性能泄漏都在糟糕的数据库设计中 - Java 类可以随时轻松更改)。
There if one functional advantage of xml files: If you have different databases with structural differences, for example one Oracle database and one MySQL database, perhaps some differences in table names and data types, then for porting your application from one database to another you only need to write some new mapping files. You do not need to change a single line of Java code. This is not possible with annotations.
xml 文件的一个功能优势是:如果您有具有结构差异的不同数据库,例如一个 Oracle 数据库和一个 MySQL 数据库,也许表名和数据类型存在一些差异,那么为了将您的应用程序从一个数据库移植到另一个数据库,您只需需要编写一些新的映射文件。您不需要更改一行 Java 代码。这对于注释是不可能的。
I prefer to use xml mapping files. That is mytaste.
我更喜欢使用 xml 映射文件。那是我的口味。
回答by beny23
One good use-case for using the XML approach is if you are persisting objects that have been generated by another utility and therefore cannot be annotated.
使用 XML 方法的一个很好的用例是,如果您要持久化由另一个实用程序生成的对象,因此无法对其进行注释。
Other than that, I would use annotations as it tends to lend itself to a cleaner implementation and you're less likely to introduce bugs by misspelling property names.
除此之外,我会使用注释,因为它往往有助于更清晰的实现,并且不太可能通过拼错属性名称来引入错误。
回答by Janaka Priyadarshana
This is how it says in "TUTORIALS POINT"
这就是“教程要点”中的说法
"If you going to make your application portable to other EJB 3 compliant ORMapplications, you must use annotations to represent the mapping information but still if you want greater flexibilitythen you should go with XML-based mappings"
“如果你想让你的应用程序移植到其他 EJB 3 兼容的 ORM应用程序,你必须使用注释来表示映射信息,但如果你想要更大的灵活性,那么你应该使用基于 XML 的映射”
For me I would prefer XML configuration file, than annotation. Because then we can do changes, with minimum code changes.
对我来说,我更喜欢 XML 配置文件,而不是注释。因为那样我们就可以进行更改,只需更改最少的代码。
回答by Oscar Castiblanco
Annotations are based on the principle of convention over configuration:
注解基于约定优于配置的原则:
http://en.wikipedia.org/wiki/Convention_over_configuration
while xml files are just configuration.
而xml文件只是配置。
There are many discussions about using annotations vs using configuration.
有很多关于使用注解和使用配置的讨论。
From my point of view, I prefer annotations because it is easier to write and to maintain.
从我的角度来看,我更喜欢注释,因为它更容易编写和维护。
回答by venkat rock
Annotations are developed with java language and java developer easy to learn when compared with XML. And one more point in real time table names and column names are fixed 99%, so no need to change java code also but if you want to change table names and column names frequently you will move with XML finally cfg.xml file is manadatary because database may change.
注解是用java语言开发的,java开发者比XML更易学。还有一点实时表名和列名是固定的99%,所以也不需要改变java代码但是如果你想经常改变表名和列名你会用XML移动最后cfg.xml文件是manadataary因为数据库可能会改变。