Hibernate implements ORM in annotated way

Source: Internet
Author: User
Tags map class

Hibernate has two ways of mapping a table into an object, a configuration file, and a way to annotate it. This is where the annotations provided by hibernate implement the correspondence between an object and a table.

Ideas:

First, configure the following in the Hibernate.cfg.xml file: database, dialect, whether SQL is displayed, load map class: Note that this hibernate.cfg.xml position is under SRC.

Because new Configuration (). Configure (). This configure () function opens the source code by default Hiberante.cfg.xml just under SRC.

The code for the configuration of Hibernate.cfg.xml is as follows:

1<?xml version= ' 1.0 ' encoding= ' utf-8 '?>2<!--3~ Hibernate, relational persistence foridiomatic Java4~5~ License:gnu Lesser general public License (LGPL), version 2.1or later.6~ See the Lgpl.txt file in the root directory or //www.gnu.org/licenses/lgpl-2.1.html>.7-8<! DOCTYPE hibernate-Configuration Public9"-//hibernate/hibernate Configuration DTD 3.0//en"Ten"Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" > One  A -  -<session-factory> the  -<!--Database connection Settings-- -<property name= "Connection.driver_class" >oracle.jdbc.OracleDriver</property> -<property name= "Connection.url" >jdbc:oracle:thin: @localhost:1521:orcl</property> +<property name= "Connection.username" >scott</property> -<property name= "Connection.password" >a123456</property> +  A<!--JDBC Connection pool (use the built-in)-- at<property name= "Connection.pool_size" >10</property> -  -<!--SQL dialect-- -<property name= "dialect" >org.hibernate.dialect.Oracle10gDialect</property> -<!--Echo all executed SQL to stdout -<property name= "Show_sql" >true</property> in  -<!--Drop and re-create the database schema on startup-- to<!--<property name= "Hbm2ddl.auto" >create</property>--> +  -<!--Names The annotated entityclass- the<mappingclass= "Com.qls.domain.DiaoChan"/> *</session-factory> $ Panax Notoginseng

Then create the Diaochan table in the Oracle database:

The SQL statements are as follows:

1 CREATE TABLE Diaochan (2  NULL , 3  Name VARCHAR2 (4),    height number (2)5   );

The Oracle database then creates a sequence that is used for the primary key generation strategy.

The SQL statements are as follows:

1 Create sequence Sixi start with 1 increment by 1;

Write a hibernate domain object:

The code is as follows:

Package Com.qls.domain;import javax.persistence.*;/** * Created by Quinlinson on 2017/5/20. */@Entity @table (name = "Diaochan") public class Diaochan {    private int id;    private String name;    private int height;    @Id   @GeneratedValue (strategy = Generationtype.sequence,generator = "People")// The contents of the upstream generator must be consistent with the name of the following line.   @SequenceGenerator (name = "People", Sequencename = "Sixi", InitialValue = 1,allocationsize = 1) public    int GetId ( ) {        return ID;    }    public void setId (int id) {        this.id = ID;    }    @Column (name = "name") public    String GetName () {        return name;    }    public void SetName (String name) {        this.name = name;    }    @Column (name = "height") public    int getheight () {        return height;    }    public void setheight (int height) {        this.height = height;    }}

Then write a test class that basically inserts a piece of data into the table:

The code is as follows:

1  Packagecom.qls.test;2 3 ImportCom.qls.domain.DiaoChan;4 Importorg.hibernate.Session;5 Importorg.hibernate.SessionFactory;6 Importorg.hibernate.Transaction;7 Importorg.hibernate.cfg.Configuration;8 9 /**Ten * Created by Quinlinson on 2017/5/20. One  */ A  Public classTest2 { -      Public Static voidMain (string[] args) { -Sessionfactory sessionfactory =NewConfiguration (). Configure (). Buildsessionfactory (); theSession session =sessionfactory.opensession (); -Diaochan Diaochan =NewDiaochan (); -Diaochan.setname ("Chongzhen"); -Diaochan.setheight (10); +Transaction tx = Session.begintransaction ();//Open the transaction.  - Session.save (Diaochan); +Tx.commit ();//commits the transaction.  ASession.close ();//closes the session.  at     } -}

After running TEST2, you will insert a piece of data in the Diaochan table:

The query results are as follows:

1 sql> select * fromDiaochan; 2  3 ID NAME                           HEIGHT4 ---------------------------------------5   3 Marten cicada                               6   4 Chongzhen                               10

The result above is the reason why I ran it two times.

Hibernate implements ORM in annotated way

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.