Spring整合hibernate自动创建表,配置如下:
<property name="hibernateProperties">
<props>
<prop key="hibernate.connection.autocommit">true</prop>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="connection.charactorEncoding">true</prop>
<prop key="show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>很奇怪,没有自动创建表,也不报错,只是在数据入库的时候,报表不存在。
我在网上找了老半天,还是没有解决问题。
我使用的是注解方式。
小弟刚在学java,请高手指教。
谢谢!
<property name="hibernateProperties">
<props>
<prop key="hibernate.connection.autocommit">true</prop>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="connection.charactorEncoding">true</prop>
<prop key="show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>很奇怪,没有自动创建表,也不报错,只是在数据入库的时候,报表不存在。
我在网上找了老半天,还是没有解决问题。
我使用的是注解方式。
小弟刚在学java,请高手指教。
谢谢!
解决方案 »
- 求问安装完j2ee之后glassfish3\jdk\lib文件夹里面为什么没有j2ee.jar?
- JAVA中如何使用JACKSON构造一个Json对象返回给前台页面,用JS接收
- hibernateTemplate 数据提交失败
- 动态加载 外部js 的问题
- 很急,在线等,请大家进来帮忙看看这个异常
- JSP页面 session 跳转到struts的action 问题
- 想学习EJB3.0的兄弟有福气了
- mybatis中删除关联表中的数据
- Weblogic受管服务器起动时有错,请问如何解决?
- j2ee j2me 哪个有钱图,学EJB要学JAVABEAN么
- http://baike.baidu.com/view/913128.htm
- update信息,过滤出被修改属性,记录更改信息日志,分数可追加
<?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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 瀵瑰彈绠$粍浠禖lasspath杩涜鎵弿 -->
<context:component-scan base-package="com.sls" /> <!-- 閰嶇疆灞炴€у崰浣嶇閰嶇疆鍣? 鍗宠鍙栭厤缃枃浠剁殑鏁版嵁搴撹繛鎺ユ枃浠?location杩欎釜鏄畾鍊?->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean> <!-- 閰嶇疆鏁版嵁搴撴簮 -->
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!--
閰嶇疆Hibernate瀹炵幇鐨凙nnotationSessionFactory宸ュ巶锛屾湰宸ュ巶鍦ㄧ郴缁熻繍琛屾湡闂村彧浜х敓涓€涓猄essionFactory瀹炰緥
-->
<bean
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
id="sessionFactory">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 閰嶇疆鍖呮壂鎻?閽堝鐨勬槸浣跨敤浜咼PA娉ㄨВ鐨勫疄浣撶被鐨?-->
<property name="packagesToScan">
<list>
<value>com.sls.entity</value>
</list>
</property>
</bean> <!-- 閰嶇疆HibernateTemplate浠ョ畝鍖朒ibernate搴旂敤鐨勫紑鍙戝嵆Hibernate妯$増-->
<bean class="org.springframework.orm.hibernate3.HibernateTemplate"
id="hibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <!-- 閰嶇疆浜嬪姟绠$悊鍣?-->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <!-- 浣跨敤鍩轰簬娉ㄨВ鏂瑰紡閰嶇疆浜嬪姟 -->
<tx:annotation-driven transaction-manager="txManager" /></beans>jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/myDataBase
jdbc.username=root
jdbc.password=root
我的基本上和你的这个是一样的,但是还是没有自动生成表。会不会是我少了什么jar包?
程序启动也不会报错。只是在程序中查询的时候,程序会报表不存在。
解决思路:
首先,我从网上查了大多数同类问题,发现我的配置与网上大多数正确的配置是一样的(除了包名不同和变量名不一样之外),排除配置问题。
其次,我重新建了一个测试工程,测试数据库是否正常,工程中只有一个实体,配置与之前的工程基本一样。同样是自动生成表,但不使用泛型(我之前的工程使用泛型的)。最后测试发现能自动创建表,进一步证明配置没有错,也证明不是数据库的问题。
最后,问题显而易见,肯定是使用泛型和实体注解有问题。把注解中不必要的部分去掉,检查泛型有没有问题。最后发现泛型有一个地方有问题,就是在Service中使用Dao时,没有使用泛型<T>,即:private TextDao textDao 改成:private TextDao<T> textDao;
再次启动服务,测试,发现能正常生成表了。问题解决。在此,谢谢各位。
非常感谢各位的支持。