为什么hibernate主键生成器无法正常工作?

时间:2015-09-17 20:55:16

标签: java spring hibernate

我有一个struts2 + spring + hibernate + maven + mysql应用程序。 所以,当我插入一个对象时,我得到了:

17.09.2015 15:41:41 *WARN * JDBCExceptionReporter: SQL Error: 0, SQLState: 07001
17.09.2015 15:41:41 *ERROR* JDBCExceptionReporter: No value specified for parameter 5
org.springframework.dao.InvalidDataAccessResourceUsageException: could not insert: [com.proximate.dashmate.models.DescargasTienda]; SQL [insert into descargas_tienda (market, descargas, actualizaciones, fecha_actualizacion, id) values (?, ?, ?, ?, ?)]; nested exception is org.hibernate.exception.SQLGrammarException: could not insert: [com.proximate.dashmate.models.DescargasTienda]
    at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:629)

当我这样做时:

@Override
public void insert(DescargasTienda o) {
    // TODO Auto-generated method stub
    getHibernateTemplate().save(o);
}

这是我的hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.show_sql">false</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <mapping resource="mappings/dashmate/DescargasTienda.hbm.xml" />
    </session-factory>
</hibernate-configuration>

这是我的映射:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 6/01/2011 05:14:47 PM by Hibernate Tools 3.2.4.GA -->
<hibernate-mapping>
    <class name="com.proximate.dashmate.models.DescargasTienda" table="descargas_tienda">
        <id name="id" type="integer">
            <column name="id"  />
            <generator class="native" />
        </id>
        <property name="market" type="string">
            <column name="market" />
        </property>
        <property name="descargas" type="integer">
            <column name="descargas" />
        </property>
        <property name="actualizaciones" type="integer">
            <column name="actualizaciones" />
        </property>
        <property name="fecha" type="java.sql.Timestamp">
            <column name="fecha_actualizacion" default="CURRENT_TIMESTAMP"/>
        </property>
    </class>    
</hibernate-mapping>

最后这是我的春天背景:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<context:annotation-config />
<context:component-scan base-package="com.proximate" />

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreUnresolvablePlaceholders">
        <value>true</value>
    </property>
    <property name="locations">
        <list>
            <value>/WEB-INF/conf/properties/jdbc.properties</value>
        </list>
    </property>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <property name="maxActive" value="10" />
    <property name="maxIdle" value="10" />
    <property name="removeAbandoned" value="true" />
    <property name="removeAbandonedTimeout" value="180" />
    <property name="validationQuery" value="SELECT 1" />
</bean>

<bean id="jpaVendorAdapter"
    class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="showSql" value="false"/>
</bean> 

<!-- Configuracion Hibernate xml -->
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation">
        <value>/WEB-INF/conf/hibernate/hibernate.cfg.xml</value>
    </property>
</bean>

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

<tx:annotation-driven transaction-manager="txManager" />

<bean id="hibernateDaoSupport" abstract="true"
    class="org.springframework.orm.hibernate3.support.HibernateDaoSupport">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="jdbcTemplateFullerette" class="org.springframework.jdbc.core.JdbcTemplate">
    <constructor-arg>
        <ref local="dataSource" />
    </constructor-arg>
</bean>

我已经尝试将我的密钥生成器从本机更改为增量,并使用完全相同的结果进行标识。 那么我做错了什么?,为什么hibernate在插入过程中没有填充我的主键,如hbm中所述?

提前致谢。

0 个答案:

没有答案