22.3. Hibernate 代码

下面的类演示了我们可以使用Hibernate对这些类进行的一些操作。

  1. package eg;
  2. import java.util.ArrayList;
  3. import java.util.Calendar;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import org.hibernate.HibernateException;
  7. import org.hibernate.Query;
  8. import org.hibernate.Session;
  9. import org.hibernate.SessionFactory;
  10. import org.hibernate.Transaction;
  11. import org.hibernate.cfg.Configuration;
  12. import org.hibernate.tool.hbm2ddl.SchemaExport;
  13. public class BlogMain {
  14. private SessionFactory _sessions;
  15. public void configure() throws HibernateException {
  16. _sessions = new Configuration()
  17. .addClass(Blog.class)
  18. .addClass(BlogItem.class)
  19. .buildSessionFactory();
  20. }
  21. public void exportTables() throws HibernateException {
  22. Configuration cfg = new Configuration()
  23. .addClass(Blog.class)
  24. .addClass(BlogItem.class);
  25. new SchemaExport(cfg).create(true, true);
  26. }
  27. public Blog createBlog(String name) throws HibernateException {
  28. Blog blog = new Blog();
  29. blog.setName(name);
  30. blog.setItems( new ArrayList() );
  31. Session session = _sessions.openSession();
  32. Transaction tx = null;
  33. try {
  34. tx = session.beginTransaction();
  35. session.persist(blog);
  36. tx.commit();
  37. }
  38. catch (HibernateException he) {
  39. if (tx!=null) tx.rollback();
  40. throw he;
  41. }
  42. finally {
  43. session.close();
  44. }
  45. return blog;
  46. }
  47. public BlogItem createBlogItem(Blog blog, String title, String text)
  48. throws HibernateException {
  49. BlogItem item = new BlogItem();
  50. item.setTitle(title);
  51. item.setText(text);
  52. item.setBlog(blog);
  53. item.setDatetime( Calendar.getInstance() );
  54. blog.getItems().add(item);
  55. Session session = _sessions.openSession();
  56. Transaction tx = null;
  57. try {
  58. tx = session.beginTransaction();
  59. session.update(blog);
  60. tx.commit();
  61. }
  62. catch (HibernateException he) {
  63. if (tx!=null) tx.rollback();
  64. throw he;
  65. }
  66. finally {
  67. session.close();
  68. }
  69. return item;
  70. }
  71. public BlogItem createBlogItem(Long blogid, String title, String text)
  72. throws HibernateException {
  73. BlogItem item = new BlogItem();
  74. item.setTitle(title);
  75. item.setText(text);
  76. item.setDatetime( Calendar.getInstance() );
  77. Session session = _sessions.openSession();
  78. Transaction tx = null;
  79. try {
  80. tx = session.beginTransaction();
  81. Blog blog = (Blog) session.load(Blog.class, blogid);
  82. item.setBlog(blog);
  83. blog.getItems().add(item);
  84. tx.commit();
  85. }
  86. catch (HibernateException he) {
  87. if (tx!=null) tx.rollback();
  88. throw he;
  89. }
  90. finally {
  91. session.close();
  92. }
  93. return item;
  94. }
  95. public void updateBlogItem(BlogItem item, String text)
  96. throws HibernateException {
  97. item.setText(text);
  98. Session session = _sessions.openSession();
  99. Transaction tx = null;
  100. try {
  101. tx = session.beginTransaction();
  102. session.update(item);
  103. tx.commit();
  104. }
  105. catch (HibernateException he) {
  106. if (tx!=null) tx.rollback();
  107. throw he;
  108. }
  109. finally {
  110. session.close();
  111. }
  112. }
  113. public void updateBlogItem(Long itemid, String text)
  114. throws HibernateException {
  115. Session session = _sessions.openSession();
  116. Transaction tx = null;
  117. try {
  118. tx = session.beginTransaction();
  119. BlogItem item = (BlogItem) session.load(BlogItem.class, itemid);
  120. item.setText(text);
  121. tx.commit();
  122. }
  123. catch (HibernateException he) {
  124. if (tx!=null) tx.rollback();
  125. throw he;
  126. }
  127. finally {
  128. session.close();
  129. }
  130. }
  131. public List listAllBlogNamesAndItemCounts(int max)
  132. throws HibernateException {
  133. Session session = _sessions.openSession();
  134. Transaction tx = null;
  135. List result = null;
  136. try {
  137. tx = session.beginTransaction();
  138. Query q = session.createQuery(
  139. "select blog.id, blog.name, count(blogItem) " +
  140. "from Blog as blog " +
  141. "left outer join blog.items as blogItem " +
  142. "group by blog.name, blog.id " +
  143. "order by max(blogItem.datetime)"
  144. );
  145. q.setMaxResults(max);
  146. result = q.list();
  147. tx.commit();
  148. }
  149. catch (HibernateException he) {
  150. if (tx!=null) tx.rollback();
  151. throw he;
  152. }
  153. finally {
  154. session.close();
  155. }
  156. return result;
  157. }
  158. public Blog getBlogAndAllItems(Long blogid)
  159. throws HibernateException {
  160. Session session = _sessions.openSession();
  161. Transaction tx = null;
  162. Blog blog = null;
  163. try {
  164. tx = session.beginTransaction();
  165. Query q = session.createQuery(
  166. "from Blog as blog " +
  167. "left outer join fetch blog.items " +
  168. "where blog.id = :blogid"
  169. );
  170. q.setParameter("blogid", blogid);
  171. blog = (Blog) q.uniqueResult();
  172. tx.commit();
  173. }
  174. catch (HibernateException he) {
  175. if (tx!=null) tx.rollback();
  176. throw he;
  177. }
  178. finally {
  179. session.close();
  180. }
  181. return blog;
  182. }
  183. public List listBlogsAndRecentItems() throws HibernateException {
  184. Session session = _sessions.openSession();
  185. Transaction tx = null;
  186. List result = null;
  187. try {
  188. tx = session.beginTransaction();
  189. Query q = session.createQuery(
  190. "from Blog as blog " +
  191. "inner join blog.items as blogItem " +
  192. "where blogItem.datetime > :minDate"
  193. );
  194. Calendar cal = Calendar.getInstance();
  195. cal.roll(Calendar.MONTH, false);
  196. q.setCalendar("minDate", cal);
  197. result = q.list();
  198. tx.commit();
  199. }
  200. catch (HibernateException he) {
  201. if (tx!=null) tx.rollback();
  202. throw he;
  203. }
  204. finally {
  205. session.close();
  206. }
  207. return result;
  208. }
  209. }