Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. public class TaskDAO {
  2. private Session session;
  3.  
  4. public TaskDAO(Session session) {
  5. this.session = session;
  6. }
  7.  
  8. public TaskDataSet get(long id) throws HibernateException {
  9. return session.get(TaskDataSet.class, id);
  10. }
  11.  
  12. public List<TaskDataSet> getAll() throws HibernateException {
  13. CriteriaBuilder builder = session.getCriteriaBuilder();
  14. CriteriaQuery<TaskDataSet> cq = builder.createQuery(TaskDataSet.class);
  15. Root<TaskDataSet> task = cq.from(TaskDataSet.class);
  16. cq.select(task);
  17. TypedQuery<TaskDataSet> q = session.createQuery(cq);
  18. List<TaskDataSet> allTask = q.getResultList();
  19. return allTask;
  20. }
  21.  
  22. public long insert(String name) {
  23. return (Long) session.save(new TaskDataSet(name));
  24. }
  25.  
  26. public void delete(long id) throws HibernateException {
  27. session.delete(get(id));
  28. }
  29. }
  30.  
  31. @Entity
  32. @Table(name="task")
  33. public class TaskDataSet {
  34.  
  35. @Id
  36. @Column(name = "id")
  37. @GeneratedValue(strategy = GenerationType.AUTO)
  38. private long id;
  39.  
  40. @Column(name = "name", unique = true, updatable = false, nullable = false)
  41. private String name;
  42.  
  43. @Column(name = "done")
  44. private boolean done;
  45.  
  46. public TaskDataSet() {}
  47.  
  48. public TaskDataSet(String name) {
  49. this.name = name;
  50. done = false;
  51. }
  52.  
  53. public long getId() {
  54. return id;
  55. }
  56.  
  57. public String getName() {
  58. return name;
  59. }
  60.  
  61. public boolean isDone() {
  62. return done;
  63. }
  64.  
  65. public void setId(int id) {
  66. this.id = id;
  67. }
  68.  
  69. public void setName(String name) {
  70. this.name = name;
  71. }
  72.  
  73. public void setDone(boolean done) {
  74. this.done = done;
  75. }
  76. }
  77.  
  78. public class DBService {
  79. private static final String hibernate_show_sql = "true";
  80. private static final String hibernate_hbm2ddl_auto = "update";
  81.  
  82. private SessionFactory sessionFactory;
  83.  
  84. public DBService() {
  85. Configuration configuration = getMySqlConfiguration();
  86. this.sessionFactory = configuration.configure().buildSessionFactory();
  87. }
  88.  
  89. private Configuration getMySqlConfiguration() {
  90. Configuration configuration = new Configuration();
  91. configuration.addAnnotatedClass(TaskDataSet.class);
  92.  
  93. configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
  94. configuration.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
  95. configuration.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/test");
  96. configuration.setProperty("hibernate.connection.username", "root");
  97. configuration.setProperty("hibernate.connection.password", "root");
  98. configuration.setProperty("hibernate.show_sql", hibernate_show_sql);
  99. configuration.setProperty("hibernate.hbm2ddl.auto", hibernate_hbm2ddl_auto);
  100. return configuration;
  101. }
  102.  
  103.  
  104. public TaskDataSet getTask(long id) throws DBException {
  105. try {
  106. Session session = sessionFactory.openSession();
  107. TaskDAO dao = new TaskDAO(session);
  108. TaskDataSet dataSet = dao.get(id);
  109. session.close();
  110. return dataSet;
  111. } catch (HibernateException e) {
  112. throw new DBException(e);
  113. }
  114. }
  115.  
  116. public List<TaskDataSet> getAllTasks() throws DBException {
  117. try {
  118. Session session = sessionFactory.openSession();
  119. TaskDAO dao = new TaskDAO(session);
  120. List<TaskDataSet> tasks = dao.getAll();
  121. return tasks;
  122. } catch (HibernateException e) {
  123. throw new DBException(e);
  124. }
  125. }
  126.  
  127. public long addTask(String name) {
  128. Session session = sessionFactory.openSession();
  129. TaskDAO dao = new TaskDAO(session);
  130. return dao.insert(name);
  131. }
  132.  
  133. public void deleteTask(long id) throws DBException {
  134. try {
  135. Session session = sessionFactory.openSession();
  136. TaskDAO dao = new TaskDAO(session);
  137. dao.delete(id);
  138. } catch (HibernateException e) {
  139. throw new DBException(e);
  140. }
  141. }
  142.  
  143. public static void main(String[] args) throws DBException {
  144. DBService dbService = new DBService();
  145. dbService.addTask("Test3");
  146.  
  147. for (TaskDataSet task : dbService.getAllTasks()) {
  148. System.out.println(task.getId());
  149. System.out.println(task.getName());
  150. System.out.println(task.isDone());
  151. System.out.println("---------------------------------");
  152. }
  153.  
  154. dbService.deleteTask(1);
  155.  
  156. for (TaskDataSet task : dbService.getAllTasks()) {
  157. System.out.println(task.getId());
  158. System.out.println(task.getName());
  159. System.out.println(task.isDone());
  160. System.out.println("---------------------------------");
  161. }
  162. }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement