Hibernate Getting Started Tutorial

Source: Internet
Author: User

Hibernate is an ORM framework that works with a variety of databases, SQL, and statements, and is a solution for data persistence.

Before we begin our study, we need to prepare our hibernate essential jar first. You can go to the official website to download: http://hibernate.org/orm/

Let's take a look at the first example of Habernate.

Development environment: MyEclipse + mysql5.5.

1. Prepare the database

We first create an employee database EMP, the database contains a table t_emp includes fields with ID, employee name, salary, age 4 fields.

Create an Employee database EMP

Create an employee table T_emp

2. Create a new EMP JavaBean class, the so-called JavaBean refers to only the attributes and the corresponding get and set methods. Our Simple object property fields correspond to the fields in our t_emp table.

1  Public classEMP {2 3     Private intId//ID4     PrivateString name;//name5     PrivateBigDecimal salary;//pay because it is money, so with bigdecimal type, more accurate. 6     Private intAge//Age7 8      PublicEmp () {9 Ten     } One      A      Public intgetId () { -         returnID; -     } the  -      Public voidSetId (intID) { -          This. ID =ID; -     } +  -      PublicString GetName () { +         returnname; A     } at  -      Public voidsetName (String name) { -          This. Name =name; -     } -  -  in      Public voidSetName (String ename) { -          This. Name =ename; to     } +  -      PublicBigDecimal getsalary () { the         returnsalary; *     } $ Panax Notoginseng      Public voidsetsalary (BigDecimal salary) { -          This. Salary =salary; the     } +  A      Public intGetage () { the         returnAge ; +     } -  $      Public voidSetage (intAge ) { $          This. Age =Age ; -     } -  the @Override -      PublicString toString () {Wuyi         return"Emp [age=" + Age + ", id=" + ID + ", name=" +name the+ ", salary=" + salary + "]"; -     } Wu}

2.hibernate Configuration

Create a new XML file in the SRC directory named Hibernate.cfg.xml

1 <!DOCTYPE hibernate-configuration Public2 "-//hibernate/hibernate Configuration DTD 3.0//en"3 "Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">4 5 <hibernate-configuration>6     <session-factory>7     8     <!--Configure the URL of a link -9     < Propertyname= "Hibernate.connection.url">Ten jdbc:mysql://localhost:3306/emp One         </ Property> A         <!-- - Configure link-driven -          - the         < Propertyname= "Hibernate.connection.driver.class"> - Com.mysql.jdbc.Driver -         </ Property> -         <!-- + Encoding Method -          - +         < Propertyname= "Connection.useunicode">True</ Property> A         < Propertyname= "Connection.characterencoding">UTF-8</ Property> at         <!-- - database user name and password -          - -         < Propertyname= "Hibernate.connection.username">Root</ Property> -         < Propertyname= "Hibernate.connection.password">Root</ Property> -         <!-- in Hiberate parse dialect, different database will be not the same, we use MySQL here -          - to         < Propertyname= "Hibernay.dialect"> + Org.hibernate.dialect.MySQLDialect.class -         </ Property> the         <!-- * Display SQL statements in the console, this is for debugging purposes, project on-line must not add this Oh $          -Panax Notoginseng         < Propertyname= "Hibernate.show_sql">True</ Property> -         < Propertyname= "Hibernate.format_sql">True</ Property> the         <!-- + introduce an EMP employee to this entity class configuration file A          - the         <MappingResource= "Entity/emp.hbm.xml"></Mapping> +     </session-factory> - </hibernate-configuration>

Now let's configure our EMP entity class and t_emp mapping relationship, create a new XML file named Emp.hbm.xml

1 <!DOCTYPE hibernate-mapping Public2 "-//hibernate/hibernate Mapping DTD 3.0//en"3 "Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">4 <hibernate-mapping>5     <classname= "ORG.TARENA.ENTITY.EMP"Table= "T_emp">6         <IDname= "id"column= "id">7             <!--The primary key generation policy for the database native configured to use the build policy that comes with the database--Reference:http://www.cnblogs.com/yfyzy/p/4675041.html> for detailed parameters8             <Generatorclass= "Native"></Generator>9         </ID>Ten         < Propertyname= "Name"column= "Name" > One         </ Property> A         < Propertyname= "Salary"column= "Salary"> -         </ Property> -         < Propertyname= "Age"column= "Age"> the         </ Property> -     </class> - </hibernate-mapping>

Below is the chart of my Project

Let's talk about it today, and continue tomorrow O (∩_∩) o~

Hibernate Getting Started Tutorial

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.