- This topic has 2 replies, 2 voices, and was last updated 15 years, 10 months ago by Loyal Water.
Viewing 3 posts - 1 through 3 (of 3 total)
-
AuthorPosts
-
kormemMemberI am new with Spring + Hibernate +icefaces with MyEclipse 7. I might have a problem with a DAO: no error message but I cannot see any data record in IceFaces page;
would you help meI am using:
– MyEclipse Derby as database
– myEclipse 7 Bench
– Spring 2.5
– Hibernate 3.2//CustomerDAO
package com; skip for import libraries public class CustomerDAO extends HibernateDaoSupport { private static final Log log = LogFactory.getLog(CustomerDAO.class); // property constants public static final String CUSTOMERNAME = "customername"; public static final String CONTACTLASTNAME = "contactlastname"; public static final String CONTACTFIRSTNAME = "contactfirstname"; public static final String PHONE = "phone"; public static final String ADDRESSLINE1 = "addressline1"; public static final String ADDRESSLINE2 = "addressline2"; public static final String CITY = "city"; public static final String STATE = "state"; public static final String POSTALCODE = "postalcode"; public static final String COUNTRY = "country"; public static final String SALESREPEMPLOYEENUMBER = "salesrepemployeenumber"; public static final String CREDITLIMIT = "creditlimit"; protected void initDao() { // do nothing } private EntityManager getEntityManager() { return EntityManagerHelper.getEntityManager(); } public void save(Customer transientInstance) { log.debug("saving Customer instance"); try { getHibernateTemplate().save(transientInstance); log.debug("save successful"); } catch (RuntimeException re) { log.error("save failed", re); throw re; } } public void delete(Customer persistentInstance) { log.debug("deleting Customer instance"); try { getHibernateTemplate().delete(persistentInstance); log.debug("delete successful"); } catch (RuntimeException re) { log.error("delete failed", re); throw re; } } public Customer update(Customer detachedInstance) { EntityManagerHelper.log("updating Customer instance", Level.INFO, null); try { Customer result = getEntityManager().merge(detachedInstance); EntityManagerHelper.log("update successful", Level.INFO, null); return result; } catch (RuntimeException re) { EntityManagerHelper.log("update failed", Level.SEVERE, re); throw re; } } public Customer findById(java.lang.Integer id) { log.debug("getting Customer instance with id: " + id); try { Customer instance = (Customer) getHibernateTemplate().get( "com.Customer", id); return instance; } catch (RuntimeException re) { log.error("get failed", re); throw re; } } public List findByExample(Customer instance) { log.debug("finding Customer instance by example"); try { List results = getHibernateTemplate().findByExample(instance); log.debug("find by example successful, result size: " + results.size()); return results; } catch (RuntimeException re) { log.error("find by example failed", re); throw re; } } public List findByProperty(String propertyName, Object value) { log.debug("finding Customer instance with property: " + propertyName + ", value: " + value); try { String queryString = "from Customer as model where model." + propertyName + "= ?"; return getHibernateTemplate().find(queryString, value); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } } public List findByCustomername(Object customername) { return findByProperty(CUSTOMERNAME, customername); } public List findByContactlastname(Object contactlastname) { return findByProperty(CONTACTLASTNAME, contactlastname); } public List findByContactfirstname(Object contactfirstname) { return findByProperty(CONTACTFIRSTNAME, contactfirstname); } public List findByPhone(Object phone) { return findByProperty(PHONE, phone); } public List findByAddressline1(Object addressline1) { return findByProperty(ADDRESSLINE1, addressline1); } public List findByAddressline2(Object addressline2) { return findByProperty(ADDRESSLINE2, addressline2); } public List findByCity(Object city) { return findByProperty(CITY, city); } public List findByState(Object state) { return findByProperty(STATE, state); } public List findByPostalcode(Object postalcode) { return findByProperty(POSTALCODE, postalcode); } public List findByCountry(Object country) { return findByProperty(COUNTRY, country); } public List findBySalesrepemployeenumber(Object salesrepemployeenumber) { return findByProperty(SALESREPEMPLOYEENUMBER, salesrepemployeenumber); } public List findByCreditlimit(Object creditlimit) { return findByProperty(CREDITLIMIT, creditlimit); } public List findAll() { log.debug("finding all Customer instances"); try { String queryString = "from Customer"; return getHibernateTemplate().find(queryString); } catch (RuntimeException re) { log.error("find all failed", re); throw re; } } public Customer merge(Customer detachedInstance) { log.debug("merging Customer instance"); try { Customer result = (Customer) getHibernateTemplate().merge( detachedInstance); log.debug("merge successful"); return result; } catch (RuntimeException re) { log.error("merge failed", re); throw re; } } public void attachDirty(Customer instance) { log.debug("attaching dirty Customer instance"); try { getHibernateTemplate().saveOrUpdate(instance); log.debug("attach successful"); } catch (RuntimeException re) { log.error("attach failed", re); throw re; } } public void attachClean(Customer instance) { log.debug("attaching clean Customer instance"); try { getHibernateTemplate().lock(instance, LockMode.NONE); log.debug("attach successful"); } catch (RuntimeException re) { log.error("attach failed", re); throw re; } } public static CustomerDAO getFromApplicationContext(ApplicationContext ctx) { return (CustomerDAO) ctx.getBean("CustomerDAO"); } }
kormemMemberadditional code;
//SessionBeanpackage org.icefaces.ui; skip for import libraries public final class SessionBean extends Object { private static boolean initialized = false; private static final Object lock = new Object(); private CustomerDAO customerDAO = new CustomerDAO(); //private ArrayList<CustomerBean> customerList = new ArrayList<CustomerBean>(); private CustomerBean selectedCustomerBean; private boolean selectedView = false; private ArrayList<CustomerBean> customerList = new ArrayList<CustomerBean>(); public SessionBean(){ customerList.addAll(customerDAO.findAll()); } public ArrayList<CustomerBean> getCustomerList() { return customerList; } public void setCustomerList(ArrayList<CustomerBean> customerList) { this.customerList = customerList; } /** * @return the customerDAO */ public CustomerDAO getCustomerDAO() { return customerDAO; } public void setCustomerDAO(CustomerDAO customerDAO) { this.customerDAO = customerDAO; } public void rowSelected(RowSelectorEvent e) { selectedCustomerBean = customerList.get(e.getRow()); selectedCustomerBean.setTempcontactfirstname(selectedCustomerBean .getCustomer().getContactfirstname()); selectedCustomerBean.setTempcontactlastname(selectedCustomerBean .getCustomer().getContactlastname()); selectedView = true; } public CustomerBean getSelectedCustomerBean() { return selectedCustomerBean; } public void setSelectedCustomerBean(CustomerBean selectedCustomerBean) { this.selectedCustomerBean = selectedCustomerBean; } public boolean isSelectedView() { return selectedView; } public void setSelectedView(boolean selectedView) { this.selectedView = selectedView; } public void commit(ActionEvent e) { selectedCustomerBean.getCustomer().setContactfirstname( selectedCustomerBean.getTempcontactfirstname()); selectedCustomerBean.getCustomer().setContactlastname( selectedCustomerBean.getTempcontactlastname()); EntityManagerHelper.beginTransaction(); customerDAO.update(selectedCustomerBean.getCustomer()); EntityManagerHelper.commit(); selectedView = !selectedView; } public void cancel(ActionEvent e) { selectedView = !selectedView; } }
//applicatoncontext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"> </property> </bean> <bean id="CustomerDAO" class="com.CustomerDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean></beans>
Loyal WaterMemberYou should try using transaction management in your code. That should help you persist the data.
-
AuthorPosts
Viewing 3 posts - 1 through 3 (of 3 total)