Trong bài cài đặt và chạy ứng dụng Hibernate sẽ giải thích làm thế nào để cài đặt Hibernate và các gói liên quan khác để chuẩn bị một môi trường phát triển cho ứng dụng Hibernate. Với ứng dụng Hibernate mình sử dụng cơ sở dữ liệu là MySQL và sử dụng Navicat là phần mền quản trị cơ sở dữ liệu MySQL.
Để có thể cài đặt và chạy ứng dụng Hibernate bạn cần phải cần có những yêu cầu cơ bản sau đây có thể kể đến như hệ quản trị cơ sở dữ liệu, thư viện Hibernate, IDE để lập trình…
1. Download Thư Viện Hibernate : linkdownload
2. Download MySQL : linkdownload
3. Download Navicat : linkdownload
Sau đây là một số hình ảnh chi tiết trong quá trình cài đặt và khời chạy ứng dụng Hibernate.
4. Tạo project và khai báo thư viện cho ứng dụng.
Tạo project trong netbean.
Đặt tên project là DemoUngDungHibernate.
Sau khi đã tạo project thì bắt đầu khai báo thư viện cho project. Click chuột phải vào project chọn proberties, chọn libraries và add thư viện mysql-connector và Hibernate mình đã download ở trên.
5. Tạo file hibernate.cfg.xml
Nội dụng trong file hibernate.cfg.xml khi mới khởi tạo.
Để hiểu rõ hơn về nội dung từng phần trong file hibernate.cfg.xml các bạn tham khảo bài viết Hướng dẫn cấu hình Hibernate Annotations, sau đây là đoạn code trong file hibernate.cfg.xml trong ứng dụng.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?xml version=‘1.0’ encoding=‘utf-8’?>
<!DOCTYPE hibernate–configuration PUBLIC
“-//Hibernate/Hibernate Configuration DTD 3.0//EN”
“http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”>
<hibernate–configuration>
<session–factory>
<property name=“hibernate.dialect”>org.hibernate.dialect.MySQLDialect</property>
<property name=“hibernate.connection.driver_class”>com.mysql.jdbc.Driver</property>
<property name=“hibernate.connection.url”>jdbc:mysql:</property>
<property name=“hibernate.connection.url”>jdbc:mysql://localhost/demoungdunghibernate?createDatabaseIfNotExist=true</property>
<property name=“hibernate.connection.username”>root</property>
<property name=“hibernate.connection.password”>root</property>
<property name=“hibernate.current_session_context_class”>thread</property>
<property name=“hibernate.hbm2ddl.auto”>update</property>
<property name=“hibernate.show_sql”>true</property>
<property name=“hibernate.format_sql”>true</property>
<mapping class=“model.TaiKhoan”/>
</session–factory>
</hibernate–configuration>
|
6. Tạo ra lớp TaiKhoan nằm trong package model.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package model;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
*
* @author KenhLapTrinh
*/
@Entity(name = “taikhoan”)
public class TaiKhoan implements Serializable {
@Id
@GeneratedValue
private long idTaiKhoan;
private String hoTen;
private String tenTaiKhoan;
private String matKhau;
public TaiKhoan() {
}
public long getIdTaiKhoan() {
return idTaiKhoan;
}
public void setIdTaiKhoan(long idTaiKhoan) {
this.idTaiKhoan = idTaiKhoan;
}
public String getHoTen() {
return hoTen;
}
public void setHoTen(String hoTen) {
this.hoTen = hoTen;
}
public String getTenTaiKhoan() {
return tenTaiKhoan;
}
public void setTenTaiKhoan(String tenTaiKhoan) {
this.tenTaiKhoan = tenTaiKhoan;
}
public String getMatKhau() {
return matKhau;
}
public void setMatKhau(String matKhau) {
this.matKhau = matKhau;
}
@Override
public String toString() {
return “TaiKhoan{“ + “idTaiKhoan=” + idTaiKhoan + “, hoTen=” + hoTen + “, tenTaiKhoan=” + tenTaiKhoan + “, matKhau=” + matKhau + ‘}’;
}
}
|
7. Tạo lớp HibernateUtil trong Package util.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package util;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;
/**
* Hibernate Utility class with a convenient method to get Session Factory
* object.
*
* @author KenhLapTrinh
*/
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from standard (hibernate.cfg.xml)
// config file.
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Log the exception.
System.err.println(“Initial SessionFactory creation failed.” + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
|
Nếu bạn nào không biết tạo lớp HibernateUtil các bạn quay lại coi ở mục số 6 trong bài viết.
8. Bây giờ mình tạo ra một class để chạy ứng dụng, mình đặt tên là TestTaiKhoan nằm trong package test.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test;
import model.TaiKhoan;
import org.hibernate.Session;
import org.hibernate.Transaction;
import util.HibernateUtil;
/**
*
* @author KenhLapTrinh
*/
public class TestTaiKhoan {
public static void main(String[] args) {
Session session =HibernateUtil.getSessionFactory().getCurrentSession();
Transaction transaction=session.beginTransaction();
TaiKhoan taiKhoan=new TaiKhoan();
taiKhoan.setHoTen(“Nguyễn Văn A”);
taiKhoan.setTenTaiKhoan(“KenhLapTrinh”);
taiKhoan.setMatKhau(“12345”);
session.save(taiKhoan);
transaction.commit();
}
}
|
Đoạn code trên có ý nghĩa là tạo ra session và gán vào transaction, sau đó tạo ra một đối tượng TaiKhoan và sử dụng phương thức save() của Hibernate để thêm đối tượng TaiKhoan, sau khi thêm thì goi phương thức comit() đẩy dữ liệu đi.
Sau đây là kết quả dưới cơ sở dữ liệu.
Bài viết này các bạn chỉ cần nắm cho mình cách khi báo thư viện cho ứng dụng Hibernate, tạo file hibernate.cfg.xml, class HibernateUtil và mapping đối tượng. Trong quá trình cái đặt và chạy ứng dụng Hibernate có gì cần hỗ trợ các bạn có thể comment bên dưới.
Download source code bài viết.
Xem tiếp: Bài 4: Cấu Hình Hibernate
(Tác giả: Công Minh)