Created
October 12, 2011 07:17
-
-
Save khannedy/1280523 to your computer and use it in GitHub Desktop.
Menampilkan Data di Database ke JTree Menggunakan Hibernate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Copyright (c) 2011, StripBandunk and/or its affiliates. All rights reserved. | |
| * | |
| * http://stripbandunk.com/ | |
| * | |
| * STRIPBANDUNK PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. | |
| */ | |
| package stripbandunk.tutorial.jtreehibernate.entity; | |
| import java.io.Serializable; | |
| import javax.persistence.Entity; | |
| import javax.persistence.GeneratedValue; | |
| import javax.persistence.Id; | |
| import javax.persistence.Table; | |
| /** | |
| * | |
| * @author Eko Kurniawan Khannedy | |
| */ | |
| @Entity | |
| @Table(name = "stripbandunk_category") | |
| public class Category implements Serializable { | |
| private static final long serialVersionUID = 1L; | |
| @Id | |
| @GeneratedValue | |
| private Long id; | |
| private String name; | |
| public Long getId() { | |
| return id; | |
| } | |
| public void setId(Long id) { | |
| this.id = id; | |
| } | |
| public String getName() { | |
| return name; | |
| } | |
| public void setName(String name) { | |
| this.name = name; | |
| } | |
| @Override | |
| public boolean equals(Object obj) { | |
| if (obj == null) { | |
| return false; | |
| } | |
| if (getClass() != obj.getClass()) { | |
| return false; | |
| } | |
| final Category other = (Category) obj; | |
| if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) { | |
| return false; | |
| } | |
| return true; | |
| } | |
| @Override | |
| public int hashCode() { | |
| int hash = 3; | |
| hash = 67 * hash + (this.id != null ? this.id.hashCode() : 0); | |
| return hash; | |
| } | |
| @Override | |
| public String toString() { | |
| return id + ". " + name; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Copyright (c) 2011, StripBandunk and/or its affiliates. All rights reserved. | |
| * | |
| * http://stripbandunk.com/ | |
| * | |
| * STRIPBANDUNK PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. | |
| */ | |
| package stripbandunk.tutorial.jtreehibernate.service; | |
| import java.util.List; | |
| import stripbandunk.tutorial.jtreehibernate.entity.Category; | |
| /** | |
| * | |
| * @author Eko Kurniawan Khannedy | |
| */ | |
| public interface CategoryService { | |
| List<Category> getAll(); | |
| Category save(String name); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Copyright (c) 2011, StripBandunk and/or its affiliates. All rights reserved. | |
| * | |
| * http://stripbandunk.com/ | |
| * | |
| * STRIPBANDUNK PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. | |
| */ | |
| package stripbandunk.tutorial.jtreehibernate.service; | |
| import java.util.List; | |
| import org.hibernate.Session; | |
| import stripbandunk.tutorial.jtreehibernate.entity.Category; | |
| import stripbandunk.tutorial.jtreehibernate.helper.HibernateHelper; | |
| /** | |
| * | |
| * @author Eko Kurniawan Khannedy | |
| */ | |
| public class CategoryServiceImpl implements CategoryService { | |
| private Session session; | |
| public CategoryServiceImpl(Session session) { | |
| this.session = session; | |
| } | |
| public CategoryServiceImpl() { | |
| this.session = HibernateHelper.getSessionFactory().openSession(); | |
| } | |
| @Override | |
| public Category save(String name) { | |
| session.beginTransaction(); | |
| Category category = new Category(); | |
| category.setName(name); | |
| session.save(category); | |
| session.getTransaction().commit(); | |
| return category; | |
| } | |
| @SuppressWarnings("unchecked") | |
| @Override | |
| public List<Category> getAll() { | |
| return session.createCriteria(Category.class).list(); | |
| } | |
| public void close() { | |
| this.session.close(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Copyright (c) 2011, StripBandunk and/or its affiliates. All rights reserved. | |
| * | |
| * http://stripbandunk.com/ | |
| * | |
| * STRIPBANDUNK PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. | |
| */ | |
| package stripbandunk.tutorial.jtreehibernate.helper; | |
| import java.util.Collection; | |
| import javax.swing.tree.DefaultMutableTreeNode; | |
| import javax.swing.tree.DefaultTreeModel; | |
| import stripbandunk.tutorial.jtreehibernate.entity.Category; | |
| /** | |
| * | |
| * @author Eko Kurniawan Khannedy | |
| */ | |
| public class CategoryTreeCreator { | |
| private DefaultTreeModel model; | |
| private DefaultMutableTreeNode root; | |
| public CategoryTreeCreator(String rootName) { | |
| root = new DefaultMutableTreeNode(rootName); | |
| model = new DefaultTreeModel(root); | |
| } | |
| public void addAll(Collection<Category> collection) { | |
| for (Category category : collection) { | |
| add(category); | |
| } | |
| } | |
| public void add(Category category) { | |
| root.add(new DefaultMutableTreeNode(category)); | |
| } | |
| public DefaultTreeModel getModel() { | |
| return model; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CategoryService service = new CategoryServiceImpl(); | |
| CategoryTreeCreator creator = new CategoryTreeCreator("Category"); | |
| creator.addAll(service.getAll()); | |
| jTreeCategory.setModel(creator.getModel()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment