{"id":68,"date":"2011-09-27T22:01:55","date_gmt":"2011-09-27T12:01:55","guid":{"rendered":"http:\/\/www.alphastar.net.au\/blog\/?p=68"},"modified":"2011-09-28T12:07:38","modified_gmt":"2011-09-28T02:07:38","slug":"hibernate-test-driver","status":"publish","type":"post","link":"https:\/\/alphastar.net.au\/weblog\/2011\/09\/27\/hibernate-test-driver\/","title":{"rendered":"Hibernate test driver"},"content":{"rendered":"<p>In the last tutorial a number of Java objects were created and the values persisted to the various database tables under the management of Hibernate. Now, we will create a very simple test program to load the data back from the database and display it to the console.<\/p>\n<p>We can do this by choosing one Person object and iterating over all the associated objects that it contains.<\/p>\n<p><!--more--><\/p>\n<p>The following code will show the effect of Hibernate re-constituting objects from the database.<\/p>\n<p>[code lang=&#8221;Java&#8221;]<br \/>\npackage alphastar.main;<\/p>\n<p>import java.util.Iterator;<br \/>\nimport java.util.Set;<\/p>\n<p>import org.hibernate.Session;<br \/>\nimport org.hibernate.Transaction;<\/p>\n<p>import alphastar.model.Person;<br \/>\nimport alphastar.model.Stock;<br \/>\nimport alphastar.model.StockEvent;<\/p>\n<p>public class ReadStock {<\/p>\n<p>public void process() {<br \/>\nTransaction txn = null;<br \/>\nSession session = HibernateUtil.getSessionFactory().openSession();<br \/>\ntxn = session.beginTransaction();<br \/>\ntry {<br \/>\n\/\/ get a Person by id<br \/>\nObject a = session.get(Person.class, new Integer(2));<br \/>\nPerson p = (Person)a;<br \/>\nSystem.out.println(&#8220;Name: &#8220;+p.getName());<\/p>\n<p>Iterator i = p.getStocksHeld().iterator();<br \/>\nwhile (i.hasNext()) {<br \/>\nObject o = i.next();<br \/>\nStock stock = (Stock)o;<br \/>\nSystem.out.println(&#8220;Code: &#8220;+stock.getCode());<br \/>\nSystem.out.println(&#8220;Name: &#8220;+stock.getName());<br \/>\n\/\/ Iterate over all the stockheld instances<\/p>\n<p>Set stockEvents = stock.getStockEvents();<br \/>\nIterator i2 = stockEvents.iterator();<br \/>\nwhile (i2.hasNext()) {<br \/>\nObject o2 = i2.next();<br \/>\nStockEvent se = (StockEvent) o2;<br \/>\nSystem.out.println(&#8220;Buy Date: &#8220;+se.getBuyDate());<br \/>\nSystem.out.println(&#8220;Buy Price: &#8220;+se.getBuyPrice());<br \/>\nSystem.out.println(&#8220;Sold Date: &#8220;+se.getSoldDate());<br \/>\nSystem.out.println(&#8220;Sold Price: &#8220;+se.getSoldPrice());<br \/>\nSystem.out.println(&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;-&#8220;);<br \/>\n}<br \/>\n}<\/p>\n<p>txn.commit();<br \/>\n\/\/session.close();<br \/>\n} catch (RuntimeException e) {<br \/>\nif(session.getTransaction() != null){<br \/>\nsession.getTransaction().rollback();<br \/>\n}<br \/>\ne.printStackTrace();<br \/>\n} finally{<br \/>\nsession.flush();<br \/>\nsession.close();<br \/>\n}<br \/>\n}<\/p>\n<p>}<\/p>\n<p>[\/code]<\/p>\n<p>As seen above, we select a Person id (in this example we choose 2, which is a PK (idperson) to the Person table. The intersting part is that a top level object with object references to other classes are automatically loaded in one go.<br \/>\nSo from loading the Person object (only) we were given the associated Stock objects, and from the Stock objects, the StockEvent objects were loaded.<\/p>\n<p><strong>code<\/strong><\/p>\n<p>Place the file in the &#8220;main&#8221; directory of the project<\/p>\n<p>The file is <a href=\"http:\/\/www.alphastar.net.au\/code\/ReadStock.java\">here<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the last tutorial a number of Java objects were created and the values persisted to the various database tables under the management of Hibernate. Now, we will create a&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-68","post","type-post","status-publish","format-standard","hentry","category-coding"],"_links":{"self":[{"href":"https:\/\/alphastar.net.au\/weblog\/wp-json\/wp\/v2\/posts\/68","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alphastar.net.au\/weblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alphastar.net.au\/weblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alphastar.net.au\/weblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alphastar.net.au\/weblog\/wp-json\/wp\/v2\/comments?post=68"}],"version-history":[{"count":18,"href":"https:\/\/alphastar.net.au\/weblog\/wp-json\/wp\/v2\/posts\/68\/revisions"}],"predecessor-version":[{"id":84,"href":"https:\/\/alphastar.net.au\/weblog\/wp-json\/wp\/v2\/posts\/68\/revisions\/84"}],"wp:attachment":[{"href":"https:\/\/alphastar.net.au\/weblog\/wp-json\/wp\/v2\/media?parent=68"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alphastar.net.au\/weblog\/wp-json\/wp\/v2\/categories?post=68"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alphastar.net.au\/weblog\/wp-json\/wp\/v2\/tags?post=68"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}