很简单的一个程序,包含一个pojo类,一个xml映射文件,一个业务逻辑类,当实例化spring容器,调用这个业务逻辑类的方法,进行数据库操作时,就会报下面的错误。
og4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
Hibernate: insert into students (name, birthday) values (?, ?)
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert: [com.firstssh.bean.Students]Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: 对象名 'students' 无效。我打开数据库,发现没有这个pojo类所对应的表,那也就是为什么会报:对象名'students’无效的原因了。但是这个程序在mysql中可以运行的,换成sql server2000就不行了,不能自动的创建表。
于是,我就手动在数据库中创建了一个students表,然后运用Myeclipse的反相工程自动生成pojo类和映射文件。调用业务逻辑,这次没有报错。但是。当我把数据库中的表删除,重新运行程序,又出现了以上错误。
我感觉,出现这个错误的原因就是,hibernate不能在数据库中自动创建这个students表,但中找不到什么原因,期待高手,给小弟指点一下。applicationContext.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"
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.xsd
                    http://www.springframework.org/schema/tx
                    http://www.springframework.org/schema/tx/spring-tx.xsd
                    http://www.springframework.org/schema/context   
                    http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="url"
value="jdbc:sqlserver://localhost:1433;database=estore">
</property>
<property name="username" value="estore"></property>
<property name="password" value="123"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>com/firstssh/bean/Students.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<prop key="hibernate.hbm2ddl.auto">
creat
</prop>
<prop key="hibernate.show_sql">
true
</prop>
</props>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
<context:annotation-config/>
<context:component-scan base-package="com.firstssh"></context:component-scan>
</beans>Students.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.firstssh.bean.Students" table="students">
        <id name="id">
       <generator class="native">
       </generator>
      </id>
      <property name="name"/>
      <property name="birthday"/>
    </class>
</hibernate-mapping>

解决方案 »

  1.   


       Students.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.firstssh.bean.Students" table="students"> 
            <id name="id"> 
          <generator class="native"> 
          </generator> 
          </id> 
          <property name="name"/> 
          <property name="birthday"/> 
        </class> 
    </hibernate-mapping>这个文件生成后有没有改过?
      

  2.   

    hibernate配置文件贴出来看看。?
      

  3.   

    谢谢楼上的朋友,我这文件我改过,我看不懂它的主键生成策略,而且还报错,我就该了。我刚刚又试了一下,保持这个映射文件不变,我手动在数据库中建一个表students(字段和映射文件都对应),能够成功运行程序,但是我把表删除,重新运行程序,又报上面的错误。所以说,这个映射文件应该没问题。问题的关键是:实例化spring容器时,为何不能自动在数据库中创建表呢???
      

  4.   

    http://blog.csdn.net/luyuwww/archive/2007/08/02/1723650.aspx看看这篇文章对你有没有帮助/
      

  5.   

    hibernate配置文件整合到spring配置文件中了,就是上面那个xml文件,什么办法都试了,有人说是jdbc驱动的问题,我也换成3.0的了。
      

  6.   

    <prop key="hibernate.hbm2ddl.auto"> 
    creat 
    </prop> 
    改成
    <prop key="hibernate.hbm2ddl.auto"> 
    update
    </prop> 
      

  7.   

    <prop key="hibernate.hbm2ddl.auto">
    creat
    </prop>记得是create吧,少个e