Objective
For Java developers, annotations should not be an unfamiliar concept, as early as the javase phase, such as the @override tag overriding the parent class method or implementing an interface method, @Test tag unit test method, so we can simply interpret it as a tag with special meaning ... In the development process, we can also use annotations to replace the configuration file implementation of related features, such as Java Web Development, 3.0 version, the use of @webservlet, @WebListener and other annotations can be used to replace the Web project XML configuration file related content. And this article is about Hibernate mapping configuration file and mapping annotations contrast, both of these can realize the mapping function, in order to avoid preconceived, in this first do not elaborate which is inferior, followed by the entity class basic mapping example, respectively, with the configuration file and annotated way to achieve.
Hibernate.cfg.xml configuration file changes when used in two different ways
<?xml version= "1.0"? ><! DOCTYPE hibernate-Configuration Public"-//hibernate/hibernate Configuration DTD 3.0//en" "Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" >//127.0.0.1/web?characterecoding=utf-8</property><property name= "Connection.username" >root</property> <property name= "Connection.password" > 123456</property> <!--set the dialect, Hibernate generates SQL statements corresponding to the database type--<property name= "dialect" >org.h Ibernate.dialect.mysqldialect</property><!--1. Using the mapping file--<!--mapping configuration source file location-<mapping resource= "Demo/pojo/person.hbm.xml"/> <!--2. Using map Annotations--<!--registering Relationship mapping class--<mapping class= "Demo.pojo.Person"/></session-factory>Entity class mappingsBackground: A user table in the database person, field four, with self-increment primary key ID, name name, gendersex, age, mapping entity class is person, attribute four, ID, name,gender, age. Note that the Entity class property name and the field names in the data table are inconsistent, such as the sex and gender here, in the mapping file <property> tags in the name and Colum two attributes are written, in the annotations can not simplify the ellipsis @column , column from the translation of people should be able to know that it is the field of the corresponding database, if you do not specify a field, by default, the system will use the name attribute value in the mapping file as the field name, the annotation method will be the property name as the field name. Another thing to note is that the class attributes are case-sensitive, and the fields are case-insensitive.
Mapping configuration Files<?XML version= "1.0"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://www.hibernate.org/dtd/hib Ernate-mapping-3.0.dtd "><hibernate-mapping> <!--name is the full name of the entity class, table is the data table name - <classname= "Demo.pojo.Person"Table= "Person"> <IDname= "id"column= "id"> <!--primary key generation mode, native is to let hibernate automatically recognize - <Generatorclass= "Native"></Generator> </ID> <!--Note: The 0.name value is the attribute name in the entity class, and column is the field name in the data table; 1. When the attribute name in the entity class is the same as the corresponding data table field name, the following column can be omitted, and hibernate automatically matches, for example, the following AG e; 2. Conversely, when the attribute name in the entity class is not the same as the field name of the corresponding data table, both items must be written, such as the following gender and sex - < Propertyname= "Name"column= "Name"></ Property> < Propertyname= "Gender"column= "Sex"></ Property> < Propertyname= "Age"></ Property> </class></hibernate-mapping>
Map annotation Methods PackageDemo.pojo;ImportJavax.persistence.Column;Importjavax.persistence.Entity;ImportJavax.persistence.GeneratedValue;ImportJavax.persistence.GenerationType;Importjavax.persistence.Id;Importjavax.persistence.Table;//entity class attribute annotations, which are written uniformly on the Get method of each property@Entity//tagging entity Classes@Table (name= "person", catalog= "Web")//Name corresponds to the data table name, catalog corresponds to the database name Public classPerson {PrivateInteger ID; PrivateString name; PrivateString Gender; PrivateInteger age; @Id//Mark primary Key@Column (name= "id")//The primary key field name, which corresponds to the Class property ID, can be simplified without writing@GeneratedValue (Strategy=generationtype.auto)//primary key generation strategy, automatic recognition PublicInteger getId () {returnID; } Public voidsetId (Integer id) { This. ID =ID; } @Column (Name= "Name")//The primary key field name, which corresponds to the class property name, is the same, this line can be simplified without writing PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } @Column (Name= "Sex")//The primary key field name, which corresponds to the class attribute gender, but not the same, not omitted PublicString Getgender () {returngender; } Public voidSetgender (String gender) { This. Gender =gender; } //Omit Annotations PublicInteger getage () {returnAge ; } Public voidsetage (Integer age) { This. Age =Age ; } @Override PublicString toString () {return"Person [id=" + ID + ", name=" + name + ", gender=" + Gender + ", age=" + Age + "]"; } }
Watch out.0. In the hibernate.cfg.xml configuration file, use two mapping methods to fill in the map configuration source file location and the full name of the mapping class, one is <mapping resource= "Xx/xx/xxx", one is <mapping class= "XXX.XXX.XXX"/>;
1. When using annotations, import in entity classes does not lead to hibernate, which is introduced in the context of the JPA (Java persistence API) category, not because it is the hibernate framework, it is naturally assumed to be the class under the Hibernate, The diagram is as follows
SummaryCompared to lengthy sections of code and individual configuration files, annotations will undoubtedly appear small and convenient, especially the following association mappings (a pair of one or one-to-many, many-to-one, many-to-many), more mapping, more configuration files required, the use of annotations directly omitted these files. In-depth understanding of the framework of learning is also very helpful, many frameworks have introduced the technology of annotations, regardless of whether or not to recognize the advantages of annotations, individuals think it is necessary to master the use of annotations.
Hibernate mapping File vs mapping annotations