spring+hibernate不能自动生成数据库表

时间:2022-07-03 21:43:39
自己看视频写的代码,就是不能生成数据库表,测试类也没报错,控制台也没报错,求解?
console:
2014-4-24 15:40:26 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1034bb5: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1034bb5]; startup date [Thu Apr 24 15:40:26 CST 2014]; root of context hierarchy
2014-4-24 15:40:27 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans.xml]
2014-4-24 15:40:27 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1034bb5]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1c74f37
2014-4-24 15:40:28 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1c74f37: defining beans [org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,dataSource,sessionFactory,txManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor]; root of factory hierarchy
2014-4-24 15:40:28 org.springframework.orm.hibernate3.LocalSessionFactoryBean buildSessionFactory
信息: Building new Hibernate SessionFactory
2014-4-24 15:40:30 org.springframework.orm.hibernate3.HibernateTransactionManager afterPropertiesSet
信息: Using DataSource [org.apache.commons.dbcp.BasicDataSource@15e2075] of Hibernate SessionFactory for HibernateTransactionManager

12 个解决方案

#1


beans.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: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:component-scan base-package="cn.com.zct" />
    
      <bean id="dataSource" destroy-method="close"
        class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="oracle.jbdc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
        <property name="username" value="ssh2" />
        <property name="password" value="ssh2" />
    </bean>
 
 
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
       <property name="dataSource" ref="dataSource" />
         <property name="mappingResources">
          <list>
            <value>cn/com/zct/Employee.hbm.xml</value>
          </list>
         </property>
         <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.OracleDialect
                hibernate.hbm2ddl.auto=update
                hibernate.show_sql=true
                hibernate.format_sql=false
            </value>
         </property>
       </bean>
      <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>      
</beans>

#2


employee.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE  hibernate-mapping   
       PUBLIC  "-//Hibernate/Hibernate  Mapping  DTD//EN"   
       "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.com.zct">  
     <class
        name="Employee" table="Employee" 
    >
        <id
            name="name"
            column="name"
            type="java.lang.String" />
        <property
            name="password"
            type="java.lang.String"
            column="password"
            not-null="true"
        />
        <property
            name="gender"
            type="java.lang.String"
            column="gender"
            not-null="true"        
       />
    </class>
</hibernate-mapping>
       
       

#3


有报错没啊?

#4


引用 3 楼 rui888 的回复:
有报错没啊?
就是没报错啊,所以看不出来为什么数据库表生成不了

#5


你登录的用户没有create table的权限

#6


数据库要自己创建的吧,表可以由hibernate帮你建

#7


该回复于2014-04-24 22:18:22被管理员删除

#8


引用 5 楼 wlwlwlwl015 的回复:
你登录的用户没有create table的权限
我设置的是dba的权限呢。。

#9


引用 6 楼 u010931121 的回复:
数据库要自己创建的吧,表可以由hibernate帮你建
数据库肯定自己建啊,。就是表不会自动生成。。。

#10


突然发现完全没有在连数据库。。真心不明白

#11


搞个断点,或日志记录一下,看到哪里报错

#12


哎自己解决了,原来是驱动类写错了,找的真是累人,果然粘贴复制才是最强大的

#1


beans.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: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:component-scan base-package="cn.com.zct" />
    
      <bean id="dataSource" destroy-method="close"
        class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="oracle.jbdc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
        <property name="username" value="ssh2" />
        <property name="password" value="ssh2" />
    </bean>
 
 
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
       <property name="dataSource" ref="dataSource" />
         <property name="mappingResources">
          <list>
            <value>cn/com/zct/Employee.hbm.xml</value>
          </list>
         </property>
         <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.OracleDialect
                hibernate.hbm2ddl.auto=update
                hibernate.show_sql=true
                hibernate.format_sql=false
            </value>
         </property>
       </bean>
      <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>      
</beans>

#2


employee.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE  hibernate-mapping   
       PUBLIC  "-//Hibernate/Hibernate  Mapping  DTD//EN"   
       "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.com.zct">  
     <class
        name="Employee" table="Employee" 
    >
        <id
            name="name"
            column="name"
            type="java.lang.String" />
        <property
            name="password"
            type="java.lang.String"
            column="password"
            not-null="true"
        />
        <property
            name="gender"
            type="java.lang.String"
            column="gender"
            not-null="true"        
       />
    </class>
</hibernate-mapping>
       
       

#3


有报错没啊?

#4


引用 3 楼 rui888 的回复:
有报错没啊?
就是没报错啊,所以看不出来为什么数据库表生成不了

#5


你登录的用户没有create table的权限

#6


数据库要自己创建的吧,表可以由hibernate帮你建

#7


该回复于2014-04-24 22:18:22被管理员删除

#8


引用 5 楼 wlwlwlwl015 的回复:
你登录的用户没有create table的权限
我设置的是dba的权限呢。。

#9


引用 6 楼 u010931121 的回复:
数据库要自己创建的吧,表可以由hibernate帮你建
数据库肯定自己建啊,。就是表不会自动生成。。。

#10


突然发现完全没有在连数据库。。真心不明白

#11


搞个断点,或日志记录一下,看到哪里报错

#12


哎自己解决了,原来是驱动类写错了,找的真是累人,果然粘贴复制才是最强大的