DELETE(DELETE SQL)
Untuk yang terakhir Delete, tambahkan coding berikut ke dalam file Main.class
1
2
3
4
5
6
7
8
9
10
| case 4 : // Delete(Delete SQL) s.beginTransaction(); // Set Query SQL query = s.createQuery( "delete from org.jaco.hibernate.Users where id = :id" ); query.setParameter( "id" , 1 ); exec = query.executeUpdate(); s.getTransaction().commit(); break ; |
Jalankan file Main.class, input 4 dan lihat outputnya. Dan jangan lupa cek di tabel database apakah record berhasil dihapus.
Dan berikut ialah source code lengkap dari tiap file - file yang ada di tutorial ini.
Users.class
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
| package org.jaco.hibernate; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table (name = "users" ) public class Users { @Id @GeneratedValue (strategy = GenerationType.AUTO) @Column (name = "id" ) int id; @Column (name = "nama" ) String nama; // Getter dan Setter public void setID( int id) { this .id = id; } public int getID() { return id; } public void setNama(String nama) { this .nama = nama; } public String getNama() { return nama; } @Override public String toString() { return "ID : " +id+ " Nama : " +nama; } } |
hibernate.cfg.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| <? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" < hibernate-configuration > < session-factory > < property name = "hibernate.connection.driver_class" >com.mysql.jdbc.Driver</ property > < property name = "hibernate.connection.password" >nasigoreng</ property > < property name = "hibernate.connection.username" >root</ property > < property name = "hibernate.dialect" >org.hibernate.dialect.MySQLDialect</ property > < property name = "hibernate.show_sql" >true</ property > < property name = "hibernate.format_sql" >true</ property > < mapping class = "org.jaco.hibernate.Users" /> </ session-factory > </ hibernate-configuration > |
Users.hbm.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| <? xml version = "1.0" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" <!-- Generated Oct 17, 2015 6:42:35 PM by Hibernate Tools 3.4.0.CR1 --> < hibernate-mapping > < class name = "org.jaco.hibernate.Users" table = "USERS" > < id name = "id" type = "int" access = "field" > < column name = "ID" /> < generator class = "assigned" /> </ id > < property name = "nama" type = "java.lang.String" > < column name = "NAMA" /> </ property > </ class > </ hibernate-mapping > |
Main.class
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
| package org.jaco.hibernate; import java.util.List; import java.util.Scanner; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.cfg.Configuration; public class Main { public static void main(String[] args) { // Buat Session untuk Koneksi Database Session s = new Configuration().configure().buildSessionFactory().openSession(); // Buat Objek dari class Users Users user = new Users(); // Pilih jenis operasi CRUD System.out.println( "Pilih Operasi 1CRUD" ); System.out.println( "1. Create" ); System.out.println( "2. Read" ); System.out.println( "3. Update" ); System.out.println( "4. Delete" ); System.out.print( "Pilihan : " ); int pilih = new Scanner(System.in).nextInt(); switch (pilih) { case 1 : // Create(Insert SQL) // set nilai untuk objek user // user.setID(null) nggak perlu dibuat karena, Auto_Increment user.setNama( "Yudi" ); try { // Mulai Koneksi s.beginTransaction(); // Simpan Objek User ke Session s.save(user); // execute Session ke MySQL s.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); } break ; case 2 : // Read(Select SQL) s.beginTransaction(); for (Users us :getAllUsers()) { System.out.println(us); } break ; case 3 : // Update(Update SQL) s.beginTransaction(); // Set Query SQL Query query = s.createQuery( "update org.jaco.hibernate.Users set nama = :nama where id = :id" ); query.setParameter( "nama" , "Setiawan" ); query.setParameter( "id" , 1 ); int exec = query.executeUpdate(); s.getTransaction().commit(); break ; case 4 : // Delete(Delete SQL) s.beginTransaction(); // Set Query SQL query = s.createQuery( "delete from org.jaco.hibernate.Users where id = :id" ); query.setParameter( "id" , 1 ); exec = query.executeUpdate(); s.getTransaction().commit(); break ; default : System.out.println( "Pilihan tidak tersedia" ); } } // Method untuk select all from table public static List<Users> getAllUsers() { List<Users> list = null ; Session session = new Configuration().configure().buildSessionFactory().openSession(); try { session.beginTransaction(); Query query = session.createQuery( "from org.jaco.hibernate.Users" ); list = query.list(); return list; } catch (Exception e) { e.printStackTrace(); return null ; } } } |
Dan berikut ialah struktur direktori dari tutorial berikut.
Sekian untuk tutorial ini
Tidak ada komentar:
Posting Komentar