15. hibernate introduction

Upload: nguyen-hung

Post on 09-Oct-2015

19 views

Category:

Documents


0 download

DESCRIPTION

hibernate 1

TRANSCRIPT

  • GII THIU HIBERNATE

    CHUYN JAVA

    Nguyn Hong Anh Email: [email protected]

    [email protected]

    H KHTN, 2011

  • Nguyn Hong Anh [email protected] H KHTN - 2011

    Ni dung trnh by

    Gii thiu Hibernate

    Xy dng ng dng qun l sinh vin n gin vi Hibernate

    Ly danh sch sinh vin

    Ly thng tin sinh vin

    Thm sinh vin

    Cp nht sinh vin

    Xa sinh vin

    2

  • Nguyn Hong Anh [email protected] H KHTN - 2011

    Hibernate

    C s d liu thng c thit k v lu tr theo hng quan h.

    Tuy nhin phn mm thng c xy dng theo hng i tng.

    i vi lp trnh vin khi xy dng phn mm thng mun lm vic vi cc i tng v khng phi nh n cc dng , cc ct trong cc bng ca c s d liu.

    3

  • Nguyn Hong Anh [email protected] H KHTN - 2011

    Ci t

    4

  • Nguyn Hong Anh [email protected] H KHTN - 2011

    Kin trc Hibernate

    5

    Code

    Hibernate Mappings Configuaration

    POJOs

    JDBC

    DATABASE

  • Nguyn Hong Anh [email protected] H KHTN - 2011

    Cc bc s dng hibernate

    Bc 1: To c s d liu

    Bc 2: To cc POJO

    Bc 3: To file cu hnh hibernate.cfg.xml

    Bc 4: To cc file mapping .hbm.xml

    Bc 5: Khai bo cc file mapping vo hibernate.cfg.xml

    Bc 6: Xy dng lp HibernateUtil

    Bc 7: Xy dng cc DAO & S dng

    6

  • Nguyn Hong Anh [email protected] H KHTN - 2011

    Bc 1: To c s d liu

    7

    CSDL: MySQL

    QuanLySinhVien

  • Nguyn Hong Anh [email protected] H KHTN - 2011

    Bc 2: To POJO SinhVien

    8

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    package pojo; import java.util.Date;

    public class SinhVien implements java.io.Serializable {

    private String maSinhVien;

    private String hoVaTen;

    private Date ngaySinh;

    private String diaChi;

    public SinhVien() {

    }

    public SinhVien(String maSinhVien) {

    this.maSinhVien = maSinhVien;

    }

    public SinhVien(String maSinhVien, String hoVaTen, Date

    ngaySinh, String diaChi) {

    this.maSinhVien = maSinhVien;

    this.hoVaTen = hoVaTen;

    this.ngaySinh = ngaySinh;

    this.diaChi = diaChi;

    }

  • Nguyn Hong Anh [email protected] H KHTN - 2011

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    public String getMaSinhVien() {

    return this.maSinhVien;

    } public void setMaSinhVien(String maSinhVien) {

    this.maSinhVien = maSinhVien;

    } public String getHoVaTen() {

    return this.hoVaTen;

    } public void setHoVaTen(String hoVaTen) {

    this.hoVaTen = hoVaTen;

    } public Date getNgaySinh() {

    return this.ngaySinh;

    } public void setNgaySinh(Date ngaySinh) {

    this.ngaySinh = ngaySinh;

    } public String getDiaChi() {

    return this.diaChi;

    } public void setDiaChi(String diaChi) {

    this.diaChi = diaChi;

    }

    }

    Bc 2: To POJO SinhVien

    9

  • Nguyn Hong Anh [email protected] H KHTN - 2011

    Bc 3: To file cu hnh hibernate.cfg.xml

    10

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    . . .

  • Nguyn Hong Anh [email protected] H KHTN - 2011

    Bc 3: To file cu hnh hibernate.cfg.xml

    11

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    org.hibernate.dialect.MySQLDialect

    com.mysql.jdbc.Driver

    jdbc:mysql://localhost:3306/QuanLySinhVien?autoReconnect&

    useUnicode=true&characterEncoding=UTF-8

    root

    root

  • Nguyn Hong Anh [email protected] H KHTN - 2011

    Bc 4: To cc file mapping SinhVien.hbm.xml

    12

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    ...

  • Nguyn Hong Anh [email protected] H KHTN - 2011

    Bc 4: To file mapping SinhVien.hbm.xml

    13

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

  • Nguyn Hong Anh [email protected] H KHTN - 2011

    Bc 5: Khai bo mapping vo hibernate.cfg.xml

    14

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    ...

  • Nguyn Hong Anh [email protected] H KHTN - 2011

    Bc 6: Xy dng lp HibernateUtil

    15

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    import org.hibernate.cfg.AnnotationConfiguration;

    import org.hibernate.SessionFactory;

    public class HibernateUtil {

    private static final SessionFactory sessionFactory;

    static {

    try {

    sessionFactory = new

    AnnotationConfiguration().configure().buildSessionFactory();

    } catch (Throwable ex) {

    System.err.println("Initial SessionFactory

    creation failed." + ex);

    throw new ExceptionInInitializerError(ex);

    }

    }

    public static SessionFactory getSessionFactory() {

    return sessionFactory;

    }

    }

  • Nguyn Hong Anh [email protected] H KHTN - 2011

    Bc 7 : Xy dng lp SinhVienDAO & s dng

    16

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    public class SinhVienDAO {

    public static List layDanhSachSinhVien() {

    List ds = null;

    Session session = HibernateUtil.getSessionFactory()

    .openSession();

    try {

    String hql = "select sv from SinhVien sv";

    Query query = session.createQuery(hql);

    ds = query.list();

    } catch (HibernateException ex) {

    //Log the exception

    System.err.println(ex);

    } finally {

    session.close();

    }

    return ds;

    }

    . . .

  • Nguyn Hong Anh [email protected] H KHTN - 2011

    Bc 7 : Xy dng lp SinhVienDAO & s dng

    17

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    public class Main {

    public static void main(String[] args) {

    //

    List ds=SinhVienDAO.layDanhSachSinhVien();

    for(int i=0; i

  • Nguyn Hong Anh [email protected] H KHTN - 2011

    Bc 7 : Xy dng lp SinhVienDAO & s dng

    18

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    public class SinhVienDAO {

    public static SinhVien layThongTinhSinhVien(String

    maSinhVien) {

    SinhVien sv = null;

    Session session = HibernateUtil.getSessionFactory()

    .openSession();

    try {

    sv = (SinhVien) session.get(SinhVien.class,

    maSinhVien);

    } catch (HibernateException ex) {

    //Log the exception

    System.err.println(ex);

    } finally {

    session.close();

    }

    return sv;

    }

    . . .

  • Nguyn Hong Anh [email protected] H KHTN - 2011

    Bc 7 : Xy dng lp SinhVienDAO & s dng

    19

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    public class Main {

    public static void main(String[] args) {

    //

    SinhVien sv =

    SinhVienDAO.layThongTinSinhVien("0312143");

    if(sv!=null){

    System.out.println("MSSV: " + sv.getMaSinhVien());

    System.out.println("H v tn: " +

    sv.getHoVaTen());

    System.out.println("Ngy sinh: " +

    sv.getNgaySinh());

    System.out.println("a ch: " + sv.getDiaChi());

    }else{

    System.out.println("Sinh vin 0312143 khng tn

    ti");

    }

    //

  • Nguyn Hong Anh [email protected] H KHTN - 2011

    Bc 7 : Xy dng lp SinhVienDAO & s dng

    20

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    public class SinhVienDAO {

    public static boolean themSinhVien(SinhVien sv) {

    Session session = HibernateUtil.getSessionFactory().openSession();

    if (SinhVienDAO.layThongTinSinhVien(sv.getMaSinhVien())!=null) {

    return false;

    }

    Transaction transaction = null;

    try {

    transaction = session.beginTransaction();

    session.save(sv);

    transaction.commit();

    } catch (HibernateException ex) {

    //Log the exception

    transaction.rollback();

    System.err.println(ex);

    } finally {

    session.close();

    }

    return true;

    }

  • Nguyn Hong Anh [email protected] H KHTN - 2011

    Bc 7 : Xy dng lp SinhVienDAO & s dng

    21

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    public class Main {

    public static void main(String[] args) {

    //

    SinhVien sv = new SinhVien();

    sv.setMaSinhVien("0312171");

    sv.setHoVaTen("T Tn Thm");

    Calendar calendar = Calendar.getInstance();

    calendar.set(1985, 5, 18);

    Date d = calendar.getTime();

    sv.setNgaySinh(d);

    sv.setDiaChi("Vng Lim Vnh Long");

    boolean kq = SinhVienDAO.themSinhVien(sv);

    if (kq == true) {

    System.out.println("Thm thnh cng");

    } else {

    System.out.println("Thm tht bi");

    }

    //

  • Nguyn Hong Anh [email protected] H KHTN - 2011

    Bc 7 : Xy dng lp SinhVienDAO & s dng

    22

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    public class SinhVienDAO { public static boolean capNhatThongTinSinhVien(SinhVien sv) {

    Session session = HibernateUtil.getSessionFactory().openSession();

    if (SinhVienDAO.layThongTinSinhVien(sv.getMaSinhVien()) == null) {

    return false;

    }

    Transaction transaction = null;

    try {

    transaction = session.beginTransaction();

    session.update(sv);

    transaction.commit();

    } catch (HibernateException ex) {

    //Log the exception

    transaction.rollback();

    System.err.println(ex);

    } finally {

    session.close();

    }

    return true;

    }

    . . .

  • Nguyn Hong Anh [email protected] H KHTN - 2011

    Bc 7 : Xy dng lp SinhVienDAO & s dng

    23

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    public class Main {

    public static void main(String[] args) {

    //

    SinhVien sv = SinhVienDAO.layThongTinSinhVien("0312143");

    if (sv != null) {

    sv.setHoVaTen("T Tn Thm");

    Calendar calendar = Calendar.getInstance();

    calendar.set(1985, 5, 18);

    Date d = calendar.getTime();

    sv.setNgaySinh(d);

    sv.setDiaChi("Vng Lim Vnh Long");

    boolean kq = SinhVienDAO.capNhatThongTinSinhVien(sv);

    if (kq == true) {

    System.out.println("Cp nht thnh cng");

    } else {

    System.out.println("Cp nht tht bi");

    }

    }

    //

  • Nguyn Hong Anh [email protected] H KHTN - 2011

    Bc 7 : Xy dng lp SinhVienDAO & s dng

    24

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    public class SinhVienDAO { public static boolean xoaSinhVien(String maSinhVien) {

    Session session = HibernateUtil.getSessionFactory().openSession();

    SinhVien sv = SinhVienDAO.layThongTinSinhVien(maSinhVien);

    if(sv==null){

    return false;

    }

    Transaction transaction = null;

    try {

    transaction = session.beginTransaction();

    session.delete(sv);

    transaction.commit();

    } catch (HibernateException ex) {

    //Log the exception

    transaction.rollback();

    System.err.println(ex);

    } finally {

    session.close();

    }

    return true;

    }

    }

  • Nguyn Hong Anh [email protected] H KHTN - 2011

    Bc 7 : Xy dng lp SinhVienDAO & s dng

    25

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    public class Main {

    public static void main(String[] args) {

    //

    boolean kq = SinhVienDAO.xoaSinhVien("0312143");

    if (kq == true) {

    System.out.println("Xa thnh cng");

    } else {

    System.out.println("Xa tht bi");

    }

    }

    //

    }

    }

  • Nguyn Hong Anh [email protected] H KHTN - 2011

    Ti liu tham kho

    Nguyn Hong Anh, Tp bi ging v video mn chuyn Java, 2010

    Marty Hall, Tp bi ging v Servlet, 2010

    http://courses.coreservlets.com/Course-Materials/csajsp2.html

    26

  • Nguyn Hong Anh [email protected] H KHTN - 2011

    HI V P

    27