|
| 1 | +package com.javahelps.hibernate; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.Properties; |
| 5 | + |
| 6 | +import org.hibernate.HibernateException; |
| 7 | +import org.hibernate.Session; |
| 8 | +import org.hibernate.SessionFactory; |
| 9 | +import org.hibernate.Transaction; |
| 10 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 11 | +import org.hibernate.cfg.Configuration; |
| 12 | +import org.hibernate.service.ServiceRegistry; |
| 13 | + |
| 14 | +public class Main { |
| 15 | + // Create the SessionFactory when you start the application. |
| 16 | + private static final SessionFactory SESSION_FACTORY; |
| 17 | + |
| 18 | + /** |
| 19 | + * Initialize the SessionFactory instance. |
| 20 | + */ |
| 21 | + static { |
| 22 | + // Create a Configuration object. |
| 23 | + Configuration config = new Configuration(); |
| 24 | + // Configure using the application resource named hibernate.cfg.xml. |
| 25 | + config.configure(); |
| 26 | + // Extract the properties from the configuration file. |
| 27 | + Properties prop = config.getProperties(); |
| 28 | + |
| 29 | + // Create StandardServiceRegistryBuilder using the properties. |
| 30 | + StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder(); |
| 31 | + builder.applySettings(prop); |
| 32 | + |
| 33 | + // Build a ServiceRegistry |
| 34 | + ServiceRegistry registry = builder.build(); |
| 35 | + |
| 36 | + // Create the SessionFactory using the ServiceRegistry |
| 37 | + SESSION_FACTORY = config.buildSessionFactory(registry); |
| 38 | + } |
| 39 | + |
| 40 | + public static void main(String[] args) { |
| 41 | + // Create two Students |
| 42 | + create(1, "Alice", 22); // Alice will get an id 1 |
| 43 | + create(2, "Bob", 20); // Bob will get an id 2 |
| 44 | + create(3, "Charlie", 25); // Charlie will get an id 3 |
| 45 | + |
| 46 | + // Update the age of Bob using the id |
| 47 | + upate(2, "Bob", 25); |
| 48 | + |
| 49 | + // Delete the Alice from database |
| 50 | + delete(1); |
| 51 | + |
| 52 | + // Print all the Students |
| 53 | + List<Student> students = readAll(); |
| 54 | + if (students != null) { |
| 55 | + for (Student stu : students) { |
| 56 | + System.out.println(stu); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // NEVER FORGET TO CLOSE THE SESSION_FACTORY |
| 61 | + SESSION_FACTORY.close(); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Create a new Student. |
| 66 | + * |
| 67 | + * @param name |
| 68 | + * @param age |
| 69 | + */ |
| 70 | + public static void create(int id, String name, int age) { |
| 71 | + // Create a session |
| 72 | + Session session = SESSION_FACTORY.openSession(); |
| 73 | + Transaction transaction = null; |
| 74 | + try { |
| 75 | + // Begin a transaction |
| 76 | + transaction = session.beginTransaction(); |
| 77 | + Student stu = new Student(); |
| 78 | + stu.setId(id); |
| 79 | + stu.setName(name); |
| 80 | + stu.setAge(age); |
| 81 | + // Save the student |
| 82 | + session.save(stu); |
| 83 | + // Commit the transaction |
| 84 | + transaction.commit(); |
| 85 | + } catch (HibernateException ex) { |
| 86 | + // If there are any exceptions, roll back the changes |
| 87 | + if (transaction != null) { |
| 88 | + transaction.rollback(); |
| 89 | + } |
| 90 | + // Print the Exception |
| 91 | + ex.printStackTrace(); |
| 92 | + } finally { |
| 93 | + // Close the session |
| 94 | + session.close(); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Read all the Students. |
| 100 | + * |
| 101 | + * @return a List of Students |
| 102 | + */ |
| 103 | + public static List<Student> readAll() { |
| 104 | + List<Student> students = null; |
| 105 | + // Create a session |
| 106 | + Session session = SESSION_FACTORY.openSession(); |
| 107 | + Transaction transaction = null; |
| 108 | + try { |
| 109 | + // Begin a transaction |
| 110 | + transaction = session.beginTransaction(); |
| 111 | + students = session.createQuery("FROM Student").list(); |
| 112 | + // Commit the transaction |
| 113 | + transaction.commit(); |
| 114 | + } catch (HibernateException ex) { |
| 115 | + // If there are any exceptions, roll back the changes |
| 116 | + if (transaction != null) { |
| 117 | + transaction.rollback(); |
| 118 | + } |
| 119 | + // Print the Exception |
| 120 | + ex.printStackTrace(); |
| 121 | + } finally { |
| 122 | + // Close the session |
| 123 | + session.close(); |
| 124 | + } |
| 125 | + return students; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Delete the existing Student. |
| 130 | + * |
| 131 | + * @param id |
| 132 | + */ |
| 133 | + public static void delete(int id) { |
| 134 | + // Create a session |
| 135 | + Session session = SESSION_FACTORY.openSession(); |
| 136 | + Transaction transaction = null; |
| 137 | + try { |
| 138 | + // Begin a transaction |
| 139 | + transaction = session.beginTransaction(); |
| 140 | + // Get the Student from the database. |
| 141 | + Student stu = (Student) session.get(Student.class, Integer.valueOf(id)); |
| 142 | + // Delete the student |
| 143 | + session.delete(stu); |
| 144 | + // Commit the transaction |
| 145 | + transaction.commit(); |
| 146 | + } catch (HibernateException ex) { |
| 147 | + // If there are any exceptions, roll back the changes |
| 148 | + if (transaction != null) { |
| 149 | + transaction.rollback(); |
| 150 | + } |
| 151 | + // Print the Exception |
| 152 | + ex.printStackTrace(); |
| 153 | + } finally { |
| 154 | + // Close the session |
| 155 | + session.close(); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Update the existing Student. |
| 161 | + * |
| 162 | + * @param id |
| 163 | + * @param name |
| 164 | + * @param age |
| 165 | + */ |
| 166 | + public static void upate(int id, String name, int age) { |
| 167 | + // Create a session |
| 168 | + Session session = SESSION_FACTORY.openSession(); |
| 169 | + Transaction transaction = null; |
| 170 | + try { |
| 171 | + // Begin a transaction |
| 172 | + transaction = session.beginTransaction(); |
| 173 | + // Get the Student from the database. |
| 174 | + Student stu = (Student) session.get(Student.class, Integer.valueOf(id)); |
| 175 | + // Change the values |
| 176 | + stu.setName(name); |
| 177 | + stu.setAge(age); |
| 178 | + // Update the student |
| 179 | + session.update(stu); |
| 180 | + |
| 181 | + // Commit the transaction |
| 182 | + transaction.commit(); |
| 183 | + } catch (HibernateException ex) { |
| 184 | + // If there are any exceptions, roll back the changes |
| 185 | + if (transaction != null) { |
| 186 | + transaction.rollback(); |
| 187 | + } |
| 188 | + // Print the Exception |
| 189 | + ex.printStackTrace(); |
| 190 | + } finally { |
| 191 | + // Close the session |
| 192 | + session.close(); |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments