Results 1 to 3 of 3

Thread: Spring Integration with Hibernate Iam not getting the exact problem Please Help

  1. #1

    Default Spring Integration with Hibernate Iam not getting the exact problem Please Help

    I am sending all codes written by me please suggest what to do to solve the Error
    package com.jlcindia.spring.hibernate;
    public class Customer {private int cid;private String cname;private String email;private long phone;private String city;public Customer() {}public Customer(String cname, String email, long phone,String city) {this.cname = cname;this.email = email;this.phone = phone;this.city = city;}setters and gettters}
    ----------------------
    package com.jlcindia.spring.hibernate;import java.util.List;
    public interface CustomerDAO {public List<CustomerTO> getCustomersByCity(String city);public List<CustomerTO> getAllCustomers1();public List<CustomerTO> getCustomersByName1(String cname);public List<CustomerTO> getCustomers1(String cname,String city);public List<CustomerTO> getAllCustomers2();public List<CustomerTO> getCustomersByName2(String cname);public List<CustomerTO> getCustomers2(String cname,String city);public int getCustomerCount();public CustomerTO getCustomerByCid(int cid);public int addCustomer(CustomerTO cto);public int updateCustomer(CustomerTO cto);public int deleteCustomer(int cid); }
    ------------------------
    package com.jlcindia.spring.hibernate;public class CustomerTO {same as Customer}
    --------------------------
    package com.jlcindia.spring.hibernate;
    import java.sql.*;import java.util.*;import org.hibernate.*;import org.hibernate.criterion.*;import org.springframework.beans.factory.annotation.Autow ired;import org.springframework.orm.hibernate3.*;
    public class HibernateCustomerDAO implements CustomerDAO{@Autowired HibernateTemplate hibernateTemp;
    public List<CustomerTO> getCustomersByCity(final String city) {List<CustomerTO> ctoList=new ArrayList<CustomerTO>();HibernateCallback hc=new HibernateCallback() {public Object doInHibernate(Session s) throws HibernateException,SQLException {Query q=s.createQuery("from Customer cust where cust.city=?");
    q.setParameter("city", city);Criteria ct=s.createCriteria(Customer.class);ct.add(Express ion.eq("city", city));return null;}}; return null;}

    public List<CustomerTO> getAllCustomers1() {List<CustomerTO> ctoList=new ArrayList<CustomerTO>();String hql="from Customer cust";List<Customer> custList=hibernateTemp.find(hql);for(Customer c:custList){CustomerTO cto=new CustomerTO(c.getCid(),c.getCname(), c.getEmail(), c.getPhone(), c.getCity());ctoList.add(cto);
    }return ctoList;}
    public List<CustomerTO> getCustomersByName1(String cname) {List<CustomerTO> ctoList=new ArrayList<CustomerTO>();String hql="from Customer cust where cust.cname=?";List<Customer> custList=hibernateTemp.find(hql,cname);for(Custome r c:custList){CustomerTO cto=new CustomerTO(c.getCid(),c.getCname(), c.getEmail(), c.getPhone(), c.getCity());ctoList.add(cto);}return ctoList;}

    public List<CustomerTO> getCustomers1(String cname, String city) {List<CustomerTO> ctoList=new ArrayList<CustomerTO>();String hql="from Customer cust cust.cname=? and cust.city=?";List<Customer> custList=hibernateTemp.find(hql,new Object[]{cname,city});for(Customer c:custList){CustomerTO cto=new CustomerTO(c.getCid(),c.getCname(), c.getEmail(), c.getPhone(), c.getCity());ctoList.add(cto);}return ctoList;}

    public List<CustomerTO> getAllCustomers2() {
    List<CustomerTO> ctoList=new ArrayList<CustomerTO>();DetachedCriteria dc=DetachedCriteria.forClass(Customer.class);List< Customer> custList=hibernateTemp.findByCriteria(dc);for(Cust omer c:custList){CustomerTO cto=new CustomerTO(c.getCid(),c.getCname(), c.getEmail(), c.getPhone(), c.getCity());ctoList.add(cto);}return ctoList;}

    public List<CustomerTO> getCustomersByName2(String cname) {List<CustomerTO> ctoList=new ArrayList<CustomerTO>();DetachedCriteria dc=DetachedCriteria.forClass(Customer.class);dc.ad d(Expression.eq("cname", cname));List<Customer> custList=hibernateTemp.findByCriteria(dc);for(Cust omer c:custList){
    CustomerTO cto=new CustomerTO(c.getCid(),c.getCname(), c.getEmail(), c.getPhone(), c.getCity());
    ctoList.add(cto);
    }return ctoList;}
    public List<CustomerTO> getCustomers2(String cname, String city) {
    List<CustomerTO> ctoList=new ArrayList<CustomerTO>();DetachedCriteria dc=DetachedCriteria.forClass(Customer.class);dc.ad d(Expression.and(Expression.eq("cname", cname),Expression.eq("city", city)));List<Customer> custList=hibernateTemp.findByCriteria(dc);for(Cust omer c:custList){CustomerTO cto=new CustomerTO(c.getCid(),c.getCname(), c.getEmail(), c.getPhone(), c.getCity());
    ctoList.add(cto);}return ctoList;}

    public int getCustomerCount() {
    String hql="from Customer cust";List<Customer> custList=hibernateTemp.find(hql);return custList.size();}
    public CustomerTO getCustomerByCid(int cid) {Customer c=(Customer)hibernateTemp.load(Customer.class, cid);
    CustomerTO cto=new CustomerTO(c.getCid(),c.getCname(), c.getEmail(), c.getPhone(), c.getCity());return cto;}
    public int addCustomer(CustomerTO cto) {Customer cust=new Customer(cto.getCname(), cto.getEmail(), cto.getPhone(), cto.getCity());return (Integer)hibernateTemp.save(cust);}
    public int updateCustomer(CustomerTO cto) {
    Customer c=(Customer)hibernateTemp.load(Customer.class, cto.getCid());c.setCname(cto.getCname());c.setEmai l(cto.getEmail());c.setPhone(cto.getPhone());
    c.setCity(cto.getCity());hibernateTemp.update(c,Lo ckMode.NONE);return cto.getCid();}
    public int deleteCustomer(int cid) {Customer c=(Customer)hibernateTemp.load(Customer.class, cid);hibernateTemp.delete(c,LockMode.NONE);return cid;}}
    ----------------------------------
    package com.jlcindia.spring.hibernate;
    import java.util.List;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlAp plicationContext;
    public class Lab4 {
    public static void main(String[] args) {
    ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext .xml");
    CustomerDAO cdao=(CustomerDAO)ctx.getBean("cdao");
    }
    }
    -----------------------------
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping package="com.jlcindia.spring.hibernate"><class name="Customer" table="customers" lazy="false"><id name="cid" column="cid" type="int"><generator class="increment"/></id>
    <property name="cname"/><property name="email"/><property name="phone" type="long"/><property name="city"/></class></hibernate-mapping>
    --------------------------------
    <?xml version="1.0" encoding="UTF-8"?><beans.....">
    <context:annotation-config/><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost/jlcindiadb"/><property name="username" value="root"/><property name="password" value="jlcindia"/></bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <property name="dataSource" ref="dataSource"/><property name="mappingResources"><list><value>com/jlcindia/spring/hibernate/Customer.hbm.xml</value></list></property><property name="hibernateProperties"><map><entry key="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
    <entry key="show_sql" value="true"/><entry key="hbm2ddl.auto" value="update"/></map></property></bean>
    <bean id="hibernateTemp" class="org.springframework.orm.hibernate3.Hibernat eTemplate" autowire="constructor"/><bean id="cdao" class="com.jlcindia.spring.hibernate.HibernateCust omerDAO"/></beans>
    --------------------------------------------------------------------------
    Now when executing it is giving the error like
    -----------------------------------------------
    log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlA pplicationContext).
    log4j:WARN Please initialize the log4j system properly.
    Exception in thread "main" java.lang.NoSuchMethodError: org.springframework.beans.factory.annotation.Injec tionMetadata.<init>(Ljava/lang/ClassV
    at org.springframework.orm.jpa.support.PersistenceAnn otationBeanPostProcessor.findPersistenceMetadata(P ersistenceAnnotationBeanPostProcessor.java:350)
    at org.springframework.orm.jpa.support.PersistenceAnn otationBeanPostProcessor.postProcessMergedBeanDefi nition(PersistenceAnnotationBeanPostProcessor.java :296)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.applyMergedBeanDefiniti onPostProcessors(AbstractAutowireCapableBeanFactor y.java:798)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:493)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 91)
    at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:222)
    at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:288 )
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:580)
    at org.springframework.context.support.AbstractApplic ationContext.finishBeanFactoryInitialization(Abstr actApplicationContext.java:895)
    at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:425)
    at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:139)
    at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:83)
    at com.jlcindia.spring.hibernate.Lab4.main(Lab4.java: 13)
    --------------------------------------
    please help.....

  2. #2
    Join Date
    Aug 2012
    Location
    Seattle
    Posts
    5

    Default

    the code that you provided was difficult to read, could you use <pre> code .. </pre> tag to represent the source codes ? please, correct me if I am wrong. It looks like you used @autowired annotation for the pojo object that does not match with xml configuration. please double check with your hibernate configuration and pojo mapping objects.

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    As mentioned before please use [ code][/code ] tags when posting code/xml/stacktraces that way we can actually read the stacktrace...

    Code:
    Exception in thread "main" java.lang.NoSuchMethodError: org.springframework.beans.factory.annotation.Injec tionMetadata.<init>(Ljava/lang/ClassV
    This indicates to me that you are mixing spring versions, don't do that...

    Also decyphering your code you are using HibernateTemplate (not related to your problem but just stating it) and that class should be considered deprecated. Simply use a plain SessionFactory to do your hibernate operations.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •