{"id":3046,"date":"2013-06-20T14:13:52","date_gmt":"2013-06-20T19:13:52","guid":{"rendered":"http:\/\/appcrawler.com\/wordpress\/?p=3046"},"modified":"2013-06-20T14:13:52","modified_gmt":"2013-06-20T19:13:52","slug":"inserting-full-objects-into-a-database-column","status":"publish","type":"post","link":"http:\/\/appcrawler.com\/wordpress\/2013\/06\/20\/inserting-full-objects-into-a-database-column\/","title":{"rendered":"Inserting full objects into a database column"},"content":{"rendered":"<p>I have never seen the need for this, but I like to understand things in case I ever run across them &#8220;in the wild&#8221;.  I did see it in a commercial ecommerce application in which the designers serialized an &#8220;Order&#8221; object into a BLOB in an Oracle database table column.<\/p>\n<p>This avoided the overhead of a straight object relational mapper such as Hibernate or the ATG repository, so I guess that is a positive.  In this case, they stored other columns such as the ORDER_ID for fast lookups, and then pulled the entire &#8220;object&#8221; back into the application server and de-serialized it.<\/p>\n<p>Below is an example:<\/p>\n<pre lang=\"java\" line=\"1\">\r\nimport java.sql.*;\r\nimport java.io.*;\r\n\r\npublic class Order implements Serializable {\r\n  int id;\r\n  int quantity;\r\n  int productId;\r\n  Order (int id, int quantity, int productId) {\r\n    this.id = id;\r\n    this.quantity = quantity;\r\n    this.productId = productId;\r\n  }\r\n  public static void main(String args[]) {\r\n    try {\r\n      Connection con = DriverManager.getConnection(\"jdbc:oracle:thin:foo\/bar@dbhost:1521\/dbservice\");\r\n      PreparedStatement pst = con.prepareStatement(\"insert into orders values(?)\");\r\n      for (int i = 1; i <= 1000; i++  ) {\r\n        Order o = new Order(i,i,i);\r\n        ByteArrayOutputStream baos = new ByteArrayOutputStream();\r\n        ObjectOutputStream objOstream = new ObjectOutputStream(baos);\r\n        objOstream.writeObject(o);\r\n        pst.setBytes(1, baos.toByteArray());\r\n        pst.execute();\r\n        con.commit();\r\n      }\r\n    }\r\n    catch(Exception e) {\r\n      e.printStackTrace();\r\n    }\r\n  }\r\n}\r\n<\/pre>\n<p>Pulling it out of the database was done in a similar fashion, but is not shown here.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have never seen the need for this, but I like to understand things in case I ever run across them &#8220;in the wild&#8221;. I did see it in a commercial ecommerce application in which the designers serialized an &#8220;Order&#8221;&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/appcrawler.com\/wordpress\/2013\/06\/20\/inserting-full-objects-into-a-database-column\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[19,24,25],"tags":[],"_links":{"self":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/3046"}],"collection":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/comments?post=3046"}],"version-history":[{"count":4,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/3046\/revisions"}],"predecessor-version":[{"id":3113,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/3046\/revisions\/3113"}],"wp:attachment":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/media?parent=3046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/categories?post=3046"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/tags?post=3046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}