public short findByName(String firstName) { tx = session.beginTransaction(); String hql = "from Customer where first_name = '" + firstName + "'"; Query query = session.createQuery(hql); Customer customer = (Customer) query.list().get(0); tx.commit(); return customer.getCustomerId(); }
@RequestMapping("/insert.do") public void insert(String firstName, String lastName, String email, Short addressId) { Customer customer = new Customer(); Address address = new Address(); customer.setStoreId(new Integer(1).byteValue()); customer.setFirstName(firstName); customer.setLastName(lastName); customer.setEmail(email); customer.setAddress(address); customer.setLastUpdate(new Date()); customer.getAddress().setAddressId(addressId); customer.setCreateDate(new Date()); try { tx = session.beginTransaction(); session.save(customer); tx.commit(); System.out.println("已经保存数据如下:"); System.out.println("ID:" + this.findByName(firstName)); System.out.println("FirstName:" + firstName); System.out.println("LastName:" + lastName); System.out.println("Email:" + email); System.out.println("Address:" + this.findAddressById(addressId)); } catch (HibernateException e) { if (tx != null) tx.rollback(); e.printStackTrace(); } finally { session.close(); } }