当前位置: 代码迷 >> J2EE >> spring+hibernate配置自动生成数据库表,总是不成功?该怎么解决
  详细解决方案

spring+hibernate配置自动生成数据库表,总是不成功?该怎么解决

热度:86   发布时间:2016-04-21 21:20:00.0
spring+hibernate配置自动生成数据库表,总是不成功??
applicationContext.xml里的配置如下:
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="admin"></property>
</bean>


<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
     <prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
    
</property>
<property name="mappingResources">
<list>
<value>com/wenruijie/dao/Users.hbm.xml</value>
</list>
</property>

com/wenruijie/dao/Users.hbm.xml里的配置如下:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.wenruijie.dao.Users" table="usersb" catalog="health">
        <id name="id" type="java.lang.String">
            <column name="id" />
            <generator class="native"></generator>
        </id>
        <property name="name" type="java.lang.String">
            <column name="Name" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="Password" />
        </property>
      
    </class>
</hibernate-mapping>
访问时不能自动生成table表,还出现如下错误
org.springframework.dao.InvalidDataAccessResourceUsageException: could not insert: [com.wenruijie.dao.Users]; SQL [insert into health.usersb (Name, Password) values (?, ?)]; nested exception is org.hibernate.exception.SQLGrammarException: could not insert: [com.wenruijie.dao.Users]
org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:629)
org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:411)
org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:683)
com.wenruijie.dao.UsersDaoImp.save(UsersDaoImp.java:22)
com.wenruijie.service.UserServiceImp.addUser(UserServiceImp.java:20)
com.wenruijie.action.UserAction.addUser(UserAction.java:31)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
到底哪配置错了????
  相关解决方案