嵌套异常是org.springframework.dao.InvalidDataAccessResourceUsageException - 无法执行查询(Spring和Hibernate)

时间:2014-07-05 09:31:32

标签: spring hibernate

我正在使用spring开发Curd操作,而我正在运行此异常无法执行查询org.hibernate.exception.SQLGrammarException 任何人都可以帮助我。

myEntity所:

@Entity
@Table(name = "person")
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
     Long id;
    @Column
    String fname;
    @Column
    String lname;
    @Column
    int age;
    @Column
    float salary;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFname() {
        return fname;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public String getLname() {
        return lname;
    }

    public void setLname(String lname) {
        this.lname = lname;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public float getSalary() {
        return salary;
    }

    public void setSalary(float salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Id: " + id + "; fname : " + fname + "' age : " + age + "; Salary: " + salary;
    }
}

的applicationContext.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
          p:location="/WEB-INF/jdbc.properties" />

    <bean id="dataSource"
          class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close"
          p:driverClassName="${jdbc.driverClassName}"
          p:url="${jdbc.url}"
          p:username="${jdbc.username}"
          p:password="${jdbc.password}" />

    <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
          <property name="dataSource" ref="dataSource"/>
          <property name="annotatedClasses">
              <list>
                  <value>training.springmvc.crud.model.Person</value>
              </list>
          </property>
          <property name="hibernateProperties">
              <value>hibernate.dialect=org.hibernate.dialect.MySQLDialect</value>
          </property>
    </bean>

    <!-- Transaction Manager -->
    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <!-- Service -->
    <bean id="personService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="target">
            <bean class="training.springmvc.crud.service.PersonServiceImpl">
                <property name="personDAO" ref="personDAO"/>
            </bean>
        </property>
        <property name="transactionAttributes">
            <props>
                <prop key="get*">PROPAGATION_SUPPORTS,readOnly</prop>
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>

    <!-- DAO -->
    <bean id="personDAO" class="training.springmvc.crud.dao.PersonDAOHib"
        p:sessionFactory="sessionFactory"
        p:sessionFactory-ref="sessionFactory"/>

</beans>

MyDao:

public class PersonDAOHib extends HibernateDaoSupport implements PersonDAO{

    public List getPersonList(){
        return getHibernateTemplate().find("from Person");
    }

    public Person getPerson(Long id){
        return (Person) getHibernateTemplate().get(Person.class, id);
    }

    public void savePerson(Person person){
        this.getHibernateTemplate().saveOrUpdate(person);
    }

    public void deletePerson(Long id){
        getHibernateTemplate().delete(getPerson(id));
    }
}

1 个答案:

答案 0 :(得分:2)

您的hibernate属性设置错误:

<propertyname="hibernateProperties">
<props>
<propkey="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<propkey="hibernate.show_sql">true</prop>
</props>
</property>