annotation annotations in Hibernate and the use of XML configuration

Source: Internet
Author: User

persistence is achieved through XML configuration :

Let's start by creating an entity class:

Package com.hibernate.model;public class student {    private int  id;    private String name;    private int  Age;        public int getid ()  {         return id;    }    public void  setid (Int id)  {        this.id = id;     }    public string getname ()  {         return name;    }    public void  setname (String name)  {        this.name =  Name;    }    public int getage ()  {          Return age;    }    public void setage (Int age)  {        this.age = age;    }         }
<?xml version= "1.0"? ><! Doctype hibernate-mapping public "-//hibernate/hibernate mapping dtd 3.0//en" "http// Www.hibernate.org/dtd/hibernate-mapping-3.0.dtd ">

The ID in the XML file indicates which column field is the primary key in the database table T_student, and the column attribute in the property is used to indicate which field of the database table the class attribute name corresponds to, and if the property name and field name have been, the column can be omitted, such as the age and name properties in the above.

At this point, just add the XML file to Hibernate's Hibernate.cfg.xml: The code in Hibernate.cfg.xml is as follows

<mapping resource= "Com/hibernate/model/student.hbm.xml"/> <!--XML file--


Persistence is achieved through annotation annotations:

first, let's create a persistent class.

package com.hibernate.model;import javax.persistence.entity;import javax.persistence.enumtype; import javax.persistence.enumerated;import javax.persistence.id;import javax.persistence.table; import javax.persistence.transient;/** *  note a bit to write on the Get () method, written in field a little violation of object-oriented programming, Because hibernate accesses the qualifier as private for this property  *  @author  administrator * */@Entity @table (name= "T_ Teacher ") public class teacher {   //class name must be consistent with the database's indication           private int id;    private String addr;     private String sex;    private String youwifename;     private zhicheng _zhicheng;   //enumeration Types          public enum zhicheng{a,b,c}  //implementing enum Types           @Enumerated (Enumtype.string)     public zhicheng get_zhicheng ()  {         return _zhicheng;    }    public void  set_zhicheng (Zhicheng _zhicheng)  {        this._ zhicheng = _zhicheng;    }              @Transient                            //is marked as transparent, indicating that the field will not be established in the database      public string getyouwifename ()  {         return youwifename;    }    public void  Setyouwifename (String youwifename)  {        this.youwifename  = youwifename;    }     @Id     public int getid ()  {         return id;    }    public  void setid (Int id)  {        this.id =  ID;&NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;PUBLIC&NBSP;STRING&NBSP;GETADDR ()  {         return addr;    }    public  VOID&NBSP;SETADDR (STRING&NBSP;ADDR)  {        this.addr =  addr;    }    public string getsex ()  {         return sex;    }     Public void setsex (String sex)  {        this.sex  = sex;    }            } 


Ok! It's easy to make annotations persistent. Next, simply add the annotated class to Hibernate.cfg.xml:

<mapping class= "Com.hibernate.model.Teacher"/> <!--annotations--


Now let's talk about the details of the annotations:

@Entity indicates that the class will use annotations for persistence
@Table (name= "T_teacher") Use this annotation when the class name and table name are inconsistent, and you can implement the teacher corresponding database table T_teacher
@Transient is marked as transparent, indicating that the field will not be established in the database
@Enumerated (enumtype.string) indicates that the field is an enumeration type


Summary: It is easier to use annotation for persistence, and it is now common for enterprises to use annotation in hibernate and seldom use XML.

annotation annotations in Hibernate and the use of XML configuration

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.