1,create a java project in eclipse.
2, add package:com.javatpoint.mypackage and Employee class:
1 packagecom.javatpoint.mypackage;2
3 public classEmployee {4
5 private intid;6 privateString firstName, lastName;7
8 public intgetId() {9 returnid;10 }11
12 public void setId(intid) {13 this.id =id;14 }15
16 publicString getFirstName() {17 returnfirstName;18 }19
20 public voidsetFirstName(String firstName) {21 this.firstName =firstName;22 }23
24 publicString getLastName() {25 returnlastName;26 }27
28 public voidsetLastName(String lastName) {29 this.lastName =lastName;30 }31
32 }
Employee.java
3,add hibernate.cfg.xml , please change the parameters in this file:
1 <?xml version="1.0" encoding="UTF-8"?>
2 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
5
6
7
8 org.hibernate.dialect.MySQLDialect
9 com.mysql.jdbc.Driver
10 jdbc:mysql://localhost:3306/test
11 hp.wang
12 123a
13
14
15
hibernate.cfg.xml
4, add employee.hbm.xml file,
1 <?xml version="1.0" encoding="UTF-8"?>
2 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
5
6
7
8
9
10
11
12
13
14
15
16
17
employee.hbm.xml
5, add the class with main():
1 packagecom.javatpoint.mypackage;2
3 importjava.util.Random;4
5 importorg.hibernate.Session;6 importorg.hibernate.SessionFactory;7 importorg.hibernate.Transaction;8 importorg.hibernate.cfg.Configuration;9
10 public classStoreData {11 public static voidmain(String[] args) {12
13 //creating configuration object
14 Configuration cfg = newConfiguration();15 cfg.configure("hibernate.cfg.xml");//populates the data of the16 //configuration file17
18 //creating seession factory object
19 SessionFactory factory =cfg.buildSessionFactory();20
21 //creating session object
22 Session session =factory.openSession();23
24 //creating transaction object
25 Transaction t =session.beginTransaction();26
27 Employee e1 = newEmployee();28 Random rd = newRandom();29 int id = rd.nextInt(50000) + 1;30 e1.setId(id);31 e1.setFirstName("firstname"+id);32 e1.setLastName("lastname"+id);33
34 session.persist(e1);//persisting the object
35
36 t.commit();//transaction is committed
37 session.close();38
39 System.out.println("successfully saved");40
41 }42 }
StoreData.java
6, create db and table in mysql:
1 CREATE TABLE`employee` (2 `id` int(11) NOT NULL,3 `firstName` varchar(50) DEFAULT NULL,4 `lastName` varchar(50) DEFAULT NULL,5 PRIMARY KEY(`id`)6 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
create table
7, run the application.
SQL Dialects in Hibernate
For connecting any hibernate application with the database, you must specify the SQL dialects. There are many Dialects classes defined for RDBMS in the org.hibernate.dialect package. They are as follows:
RDBMSDialect
Oracle (any version)
org.hibernate.dialect.OracleDialect
Oracle9i
org.hibernate.dialect.Oracle9iDialect
Oracle10g
org.hibernate.dialect.Oracle10gDialect
MySQL
org.hibernate.dialect.MySQLDialect
MySQL with InnoDB
org.hibernate.dialect.MySQLInnoDBDialect
MySQL with MyISAM
org.hibernate.dialect.MySQLMyISAMDialect
DB2
org.hibernate.dialect.DB2Dialect
DB2 AS/400
org.hibernate.dialect.DB2400Dialect
DB2 OS390
org.hibernate.dialect.DB2390Dialect
Microsoft SQL Server
org.hibernate.dialect.SQLServerDialect
Sybase
org.hibernate.dialect.SybaseDialect
Sybase Anywhere
org.hibernate.dialect.SybaseAnywhereDialect
PostgreSQL
org.hibernate.dialect.PostgreSQLDialect
SAP DB
org.hibernate.dialect.SAPDBDialect
Informix
org.hibernate.dialect.InformixDialect
HypersonicSQL
org.hibernate.dialect.HSQLDialect
Ingres
org.hibernate.dialect.IngresDialect
Progress
org.hibernate.dialect.ProgressDialect
Mckoi SQL
org.hibernate.dialect.MckoiDialect
Interbase
org.hibernate.dialect.InterbaseDialect
Pointbase
org.hibernate.dialect.PointbaseDialect
FrontBase
org.hibernate.dialect.FrontbaseDialect
Firebird
org.hibernate.dialect.FirebirdDialect
..