01 /*
02 * To change this template, choose Tools | Templates
03 * and open the template in the editor.
04 */
05
06 package org.jug.joglosemar.entity;
07
08 /**
09 *
10 * @author Wilbert
11 * Class ini melambangkan tabel Article di database
12 * yang nantinya digunakan untuk menampilkan artikel-artikel
13 */
14 public class Article
15 {
16 private long id;
17 private String title;
18 private String detail;
19 private long datePost;
20 private Jugers author;
21
22 public Article() {}
23
24 public long getId() {
25 return id;
26 }
27
28 public void setId(long id) {
29 this.id = id;
30 }
31
32 public String getTitle() {
33 return title;
34 }
35
36 public void setTitle(String title) {
37 this.title = title;
38 }
39
40 public String getDetail() {
41 return detail;
42 }
43
44 public void setDetail(String detail) {
45 this.detail = detail;
46 }
47
48 public long getDatePost() {
49 return datePost;
50 }
51
52 public void setDatePost(long datePost) {
53 this.datePost = datePost;
54 }
55
56 public Jugers getAuthor() {
57 return author;
58 }
59
60 public void setAuthor(Jugers author) {
61 this.author = author;
62 }
63 }
->>
01
02
03
04
05
06
07
08
09
10
11
12
13
->>Lalu setelah itu, saya tambahkan mapping tersebut ke dalam file konfigurasi hibernate.., yaitu di file hibernate.cfg.xml.. Berikut isi filenya :
01
02
03
04
05
10
11
12 java:/comp/env/jdbc/joglosemar
13
14 org.hibernate.dialect.MySQLDialect
15
16 thread
17
18 true
19
20 create
21
22
23
24
25
26
27
->>Dan langkah yang terakhir adalah membuat DAO + Implementasinya.. Berikut ini saya akan tampilkan DAO + Implementasi yang sudah saya buat..
01 /*
02 * To change this template, choose Tools | Templates
03 * and open the template in the editor.
04 */
05
06 package org.jug.joglosemar.dao;
07
08 import java.util.List;
09 import org.jug.joglosemar.entity.Article;
10
11 /**
12 *
13 * @author Wilbert
14 */
15 public interface ArticleDAO
16 {
17 public void insert(Article artc) throws Exception;
18 public void delete(Article artc) throws Exception;
19 public void update(Article artc) throws Exception;
20 public Article load(long id) throws Exception;
21 public List getAllArticles() throws Exception;
22 public List getLastArticles() throws Exception;
23 }
->>
01 /*
02 * To change this template, choose Tools | Templates
03 * and open the template in the editor.
04 */
05
06 package org.jug.joglosemar.dao.hbm;
07
08 import java.util.ArrayList;
09 import java.util.List;
10 import org.hibernate.Session;
11 import org.jug.joglosemar.dao.ArticleDAO;
12 import org.jug.joglosemar.entity.Article;
13 import org.jug.joglosemar.util.HibernateUtil;
14
15 /**
16 *
17 * @author Wilbert
18 * Implementasi DAO untuk query ke database
19 */
20 public class ArticleDAOImpl implements ArticleDAO
21 {
22 public void insert(Article artc) throws Exception {
23 HibernateUtil.beginTransaction();
24 Session session = HibernateUtil.getSession();
25 try{
26 session.save(artc);
27 }catch(Exception ex){
28 throw ex;
29 }
30 }
31
32 public void delete(Article artc) throws Exception {
33 HibernateUtil.beginTransaction();
34 Session session = HibernateUtil.getSession();
35 try{
36 session.delete(artc);
37 }catch(Exception ex){
38 throw ex;
39 }
40 }
41
42 public void update(Article artc) throws Exception {
43 HibernateUtil.beginTransaction();
44 Session session = HibernateUtil.getSession();
45 try{
46 session.update(artc);
47 }catch(Exception ex){
48 throw ex;
49 }
50 }
51
52 public Article load(long id) throws Exception {
53 HibernateUtil.beginTransaction();
54 Session session = HibernateUtil.getSession();
55 Article artc = null;
56 try{
57 artc = (Article)session.load(Article.class,id);
58 }catch(Exception ex){
59 throw ex;
60 }
61 return artc;
62 }
63
64 public List getAllArticles() throws Exception {
65 HibernateUtil.beginTransaction();
66 Session session = HibernateUtil.getSession();
67 List list = new ArrayList();
68 try{
69 list = session.createQuery("from Article").list();
70 }catch(Exception ex){
71 throw ex;
72 }
73 return list;
74 }
75
76 public List getLastArticles() throws Exception {
77 HibernateUtil.beginTransaction();
78 Session session = HibernateUtil.getSession();
79 List list = new ArrayList();
80 try{
81 list = session.createQuery("from Article order by date_post desc limit 0,10").list();
82 }catch(Exception ex){
83 throw ex;
84 }
85 return list;
86 }
87 }
->>Selesai sudah untuk pembuatan entitynya.. Perlu diperhatikan juga (bagi yang merasa team saya), kita bermain dengan 2 framework, yaitu Struts dan Hibernate.. Maka harus dipastikan kita bisa membedakan keduanya.. Jangan dicampur adukkan..! Sekarang kita coding dulu Hibernate nya, setelah itu baru kita bermain Struts nya..
Saya rasa kalau satu per satu cara memikirkannya malah lebih bagus, karena ada kerangka berpikir yang terstruktur, yaitu Struts dan Hibernate..