1 //---------------------------------------------------------------------- 2 3 //PerfectJPattern: "Design patterns are good but components are better!" 4 //Example.java Copyright (c) 2009 Giovanni Azua Garcia 5 //bravegag@hotmail.com 6 7 //This program is free software; you can redistribute it and/or 8 //modify it under the terms of the GNU General Public License 9 //as published by the Free Software Foundation; either version 3 10 //of the License, or (at your option) any later version. 11 12 //This program is distributed in the hope that it will be useful, 13 //but WITHOUT ANY WARRANTY; without even the implied warranty of 14 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 //GNU General Public License for more details. 16 17 //You should have received a copy of the GNU General Public License 18 //along with this program; if not, see <http://www.gnu.org/licenses/>. 19 //Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 20 //MA 02110-1301, USA. 21 22 //---------------------------------------------------------------------- 23 package org.perfectjpattern.jee.integration.dao.hibernate; 24 25 import java.text.*; 26 import java.util.*; 27 28 import org.perfectjpattern.example.datamodel.*; 29 import org.perfectjpattern.example.datamodel.visitor.*; 30 import org.perfectjpattern.jee.api.integration.dao.*; 31 import org.perfectjpattern.jee.integration.dao.*; 32 import org.slf4j.*; 33 34 /** 35 * Startup Main for the Hibernate Generic Dao example code 36 * 37 * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a> 38 * @version $ $Date: Dec 5, 2008 2:04:37 AM $ 39 */ 40 //CHECKSTYLE:OFF 41 public final 42 class Example 43 // CHECKSTYLE:ON 44 { 45 //------------------------------------------------------------------------ 46 // public 47 //------------------------------------------------------------------------ 48 public static void 49 main(String[] anArguments) 50 throws ParseException 51 { 52 // ToStringVisitor needs loading a ResourceBundle 53 Locale.setDefault(new Locale("en", "US")); 54 55 //--------------------------------------------------------------- 56 // Create fixture/ business model 57 //--------------------------------------------------------------- 58 Product myProduct1 = new Product("Nikon D300", 2000.0); 59 Product myProduct2 = new Product("Nikon D40x", 350.0); 60 Product myProduct3 = new Product("Nikon 80-400mm", 1500.0); 61 62 Customer myCustomer1 = new Customer("Ernesto"); 63 64 Date myDate = DATE_FORMAT.parse("14.03.2007 08:00:00"); 65 Set<Product> myProducts = new HashSet<Product>(); 66 myProducts.addAll(Arrays.asList(new Product[] {myProduct1, 67 myProduct2, myProduct3 })); 68 Order myOrder1 = new Order(myCustomer1, myDate, myProducts); 69 70 myCustomer1.getOrders().add(myOrder1); 71 72 try 73 { 74 //----------------------------------------------------------- 75 // Accesses the AbstractFactory singleton HibernateDaoFactory 76 // and persists Customer model in storage. Note how the 77 // HibernateDaoFactory hides the framework-specific complexities. 78 // By default it gets initialized to use: 79 // 80 // - HibernateCurrentSessionStrategy: 81 // a) Creates SessionFactory from Configuration 82 // b) Uses the getCurrentSession Hibernate API that 83 // looks into the "current_session_context_class" 84 // configuration, for this example is set to "thread" 85 // 86 // - HibernateConfiguredTransactionStrategy: 87 // Simply accesses the Session Strategy and calls 88 // getTransaction, that will load whatever is configured 89 // in "hibernate.transaction_factory" configuration 90 //----------------------------------------------------------- 91 IGenericDao<Long, Customer> myCustomerDao = HibernateDaoFactory. 92 getInstance().createDao(Customer.class); 93 myCustomerDao.create(myCustomer1); 94 95 //----------------------------------------------------------- 96 // Update the Customer model/ add new Product and Order 97 //----------------------------------------------------------- 98 Product myProduct4 = new Product("Nikon Fisheye 10mm", 900.0); 99 100 myDate = DATE_FORMAT.parse("19.10.2007 08:00:00"); 101 myProducts = new HashSet<Product>(); 102 myProducts.add(myProduct4); 103 Order myNewOrder = new Order(myCustomer1, myDate, myProducts); 104 105 myCustomer1.getOrders().add(myNewOrder); 106 myCustomerDao.update(myCustomer1); 107 108 //----------------------------------------------------------- 109 // Commit changes. Note how accessing whatever Transaction 110 // implementation is transparent to the client 111 //----------------------------------------------------------- 112 myCustomerDao.getTransaction().commit(); 113 114 //----------------------------------------------------------- 115 // Delete one Product 116 //----------------------------------------------------------- 117 myOrder1.getProducts().remove(myProduct2); 118 119 IGenericDao<Long, Order> myOrderDao = HibernateDaoFactory. 120 getInstance().createDao(Order.class); 121 myOrderDao.update(myOrder1); 122 123 //----------------------------------------------------------- 124 // Commit changes. 125 //----------------------------------------------------------- 126 myCustomerDao.getTransaction().commit(); 127 128 //----------------------------------------------------------- 129 // Clear the cache 130 //----------------------------------------------------------- 131 myCustomerDao.getSession().clear(); 132 133 //----------------------------------------------------------- 134 // Retrieve all Customers 135 //----------------------------------------------------------- 136 List<Customer> myCustomers = myCustomerDao.findAll(); 137 138 ToStringVisitor myVisitor = new ToStringVisitor(); 139 for (Customer myCustomer : myCustomers) 140 { 141 myVisitor.visit(myCustomer); 142 String myResult = myVisitor.getResult(); 143 144 theLogger.debug(myResult); 145 } 146 } 147 finally 148 { 149 //----------------------------------------------------------- 150 // Final cleanup 151 //----------------------------------------------------------- 152 HibernateDaoFactory.getInstance().shutdown(); 153 } 154 } 155 156 //------------------------------------------------------------------------ 157 // protected 158 //------------------------------------------------------------------------ 159 protected static void 160 setLogger(Logger aLogger) 161 { 162 theLogger = aLogger; 163 } 164 165 //------------------------------------------------------------------------ 166 // members 167 //------------------------------------------------------------------------ 168 /** 169 * Provides logging facilities for this class 170 */ 171 private static Logger theLogger = LoggerFactory.getLogger(Example.class); 172 private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat( 173 "dd.MM.yyyy HH:mm:ss"); 174 }