Getting Started with hibernate

Source: Internet
Author: User

Note: Hibernate: can be implemented without the use of SQL statements can be implemented to the database additions and deletions to check. That is the specific implementation of JPA

1. Create Java Project:hibernate_helloworld and click on Project Right--->myeclipse ..... Introducing Hibernate Library


3. Establish database test and table teacher, Student

4. Create an entity class, test the class

4.1 Student Class (Attribute Stuid, Stuname, stuage, corresponding Getset method)

Package Com.cqvie.model;

public class Student {
Private String Stuname;
Private Integer Stuid;
Private Integer stuage;
Public String Getstuname () {
return stuname;
}

public void Setstuname (String stuname) {
This.stuname = Stuname;
}
Public Integer Getstuid () {
return stuid;
}
public void Setstuid (Integer stuid) {
This.stuid = Stuid;
}
Public Integer Getstuage () {
return stuage;
}
public void Setstuage (Integer stuage) {
This.stuage = Stuage;
}
}

4.2 Teacher Class (attribute ID, name, title, and corresponding Getset method)

Package Com.cqvie.model;

Import javax.persistence.Entity;
Import Javax.persistence.Id;

@Entity
public class Teacher {
private String name;
Private Integer ID;
Private String title;
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
@Id
Public Integer getId () {
return ID;
}
public void SetId (Integer id) {
This.id = ID;
}
Public String GetTitle () {
return title;
}
public void Settitle (String title) {
This.title = title;
}

}

4.3 Studenttest class

Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.cfg.AnnotationConfiguration;
Import org.hibernate.cfg.Configuration;

Import com.cqvie.model.Student;

public class Studenttest {
public static void Main (string[] args) {
TODO auto-generated Method Stub
Student stu = new Student ();
STU.SETSTUID (004);
Stu.setstuname ("Anna");
Stu.setstuage (10);

Configuration cfg = new configuration ();
Sessionfactory sf=cfg.configure (). Buildsessionfactory ();//Read file
Session session =sf.opensession ();//Open a new session
Session.begintransaction ();//Put in a thing
Session.save (Stu);

Session.delete (stu);//Delete the record

Session.update (stu);//Modify this record


Session.gettransaction (). commit ();
Session.close ();
Sf.close ();
}
}

The result can be seen at this point where the record is inserted into the table

And then look at the backstage has printed a message

4.4 Teachertest class Add a record

Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.cfg.AnnotationConfiguration;
Import org.hibernate.cfg.Configuration;

Import Com.cqvie.model.Teacher;

public class Teachertest {
public static void Main (string[] args) {
TODO auto-generated Method Stub
Teacher t = new Teacher ();
T.setid (1);
T.setname ("Chongqing");
T.settitle ("Hello");
@SuppressWarnings ("deprecation")
Configuration cfg = new annotationconfiguration ();
@SuppressWarnings ("deprecation")
Sessionfactory sf=cfg.configure (). Buildsessionfactory ();//Read file
Session session =sf.opensession ();//Open a new session
Session.begintransaction ();//Put in a thing
Session.save (t);
Session.gettransaction (). commit ();//Commit The Thing
Session.close ();
Sf.close ();
}
}

T.setid (1);
T.setname ("Chongqing");
T.settitle ("Hello");

The three fields you set are already inserted into the table.

5. Modify the Hibernate.cfg.xml file: modify the corresponding database name test, connect to the database user name (root) and password (empty), the corresponding mapping path should also be modified, note the format.

<?xml version= ' 1.0 ' encoding= ' UTF-8 '?>
<! DOCTYPE hibernate-configuration Public
"-//hibernate/hibernate Configuration DTD 3.0//en"
"Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" >
<!--Generated by MyEclipse Hibernate Tools. -
<session-factory>
<!--Database connection Settings--
<property name= "Connection.driver_class" >com.mysql.jdbc.Driver</property>
<property name= "Connection.url" >jdbc:mysql://localhost/test</property>
<property name= "Connection.username" >root</property>
<property name= "Connection.password" ></property>

<!--JDBC Connection pool (use the built-in)--
<property name= "Connection.pool_size" >1</property>

<!--SQL dialect--
<property name= "dialect" >org.hibernate.dialect.MySQLDialect</property>

<!--Enable Hibernate ' s automatic session context management--
<property name= "Current_session_context_class" >thread</property>

<!--Disable the Second-level cache--and
<property name= "Cache.provider_class" >org.hibernate.cache.NoCacheProvider</property>

<!--Echo all executed SQL to stdout
<property name= "Show_sql" >true</property>

<!--Drop and re-create the database schema on startup--
<property name= "Hbm2ddl.auto" >update</property>

<mapping resource= "Com/cqvie/model/student.hbm.xml"/>
<mapping class= "Com.cqvie.model.Teacher"/>
</session-factory>

The entity class student the corresponding mapping file Student.hbm.xml (note the written format, otherwise the corresponding file will not be found.) )

<?xml version= "1.0"?>
<! DOCTYPE hibernate-mapping Public
"-//hibernate/hibernate Mapping DTD 3.0//en"
"Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<class name= "Student" >
<id name= "Stuid" ></id>
<property name= "Stuname" ></property>
<property name= "Stuage" ></property>
</class>

6. Introduce the MySQL driver (create a new Libs folder and/hibernate_helloworld/mysql-connector-java-5.1.24-
Bin.jar put inside remember must add it to bulid path inside, otherwise can't load drive MySQL)

7. Summary and Lessons learned

7.1 Hibername can generate a data table from an entity class, just run a program to generate a database table
7.2 It should be said that the entity class should be written first, and then the table (object-oriented); But in real life, more is to build the table and then generate the entity class, because the table has some necessary index, need to optimize, the first table can improve efficiency.

Analysis of Hbm2ddl.auto:4 species in 7.3

<property name= "Hbm2ddl.auto" >CREATE</PROPERTY>: Create can be implemented as long as the entity class is created and configured
HBM files to create the corresponding database tables.

<property name= "Hbm2ddl.auto" >CREATE-DROP</PROPERTY>: means that database tables that exist in the database are deleted (
Fields and records that include tables)

<property name= "Hbm2ddl.auto" >VALIDATE</PROPERTY>: it means that before all the database tables are manipulated, hibernate will
Queries the database for the existence of the corresponding database table.

<property name= "Hbm2ddl.auto" >UPDATE</PROPERTY>: equivalent to update a new record, the primary key is not the same, when
When the primary key is consistent, it does not perform any action and will also error

Getting Started with hibernate

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.