---
title: "使用Hibernate进行数据查询却清除了数据库对应表的内容"
author: "xycdgc"
date: "2010-04-19T22:09:51Z"
tags: []
categories: ["企业架构"]
slug: "hibernate"
draft: false
lastmod: "2010-04-27T15:44:23Z"
id: 647825
url: "https://xycdgc.iteye.com/blog/647825"
---

这些天，在运用Hibernate框架的时候，出现了一个问题，我在插入数据的时候，能够正确的运行，一查询数据，即使是最简单的将一个表中的所有数据查询出来都不能成功，最郁闷的是，经过查询后，还把数据库中表中数据全部清除了，将hibernate下的log4j.properties引入后，查看日志： 22:04:59,359 INFO Environment:479 - Hibernate 3.1.2 22:04:59,390 INFO Environment:509 - hibernate.properties not found 22:04:59,390 INFO Environment:525 - using CGLIB reflection optimizer 22:04:59,390 INFO Environment:555 - using JDK 1.4 java.sql.Timestamp handling 22:04:59,468 INFO Configuration:1342 - configuring from file: hibernate.cfg.xml 22:05:12,687 INFO Configuration:469 - Reading mappings from resource: ch3/User.hbm.xml 22:05:22,890 INFO HbmBinder:309 - Mapping class: ch3.User -&gt; myusertable 22:05:23,187 INFO Configuration:1419 - Configured SessionFactory: null 22:05:24,375 INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!) 22:05:24,375 INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 20 22:05:24,375 INFO DriverManagerConnectionProvider:45 - autocommit mode: false 22:05:24,531 INFO DriverManagerConnectionProvider:80 - using driver: com.microsoft.jdbc.sqlserver.SQLServerDriver at URL: jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mypro 22:05:24,531 INFO DriverManagerConnectionProvider:86 - connection properties: {user=sa, password=\*\*\*\*} 22:05:25,734 INFO SettingsFactory:77 - RDBMS: Microsoft SQL Server, version: Microsoft SQL Server 2000 - 8.00.2039 (Intel X86) May 3 2005 23:18:38 Copyright (c) 1988-2003 Microsoft Corporation Personal Edition on Windows NT 5.1 (Build 2600: Service Pack 2) 22:05:25,734 INFO SettingsFactory:78 - JDBC driver: SQLServer, version: 2.2.0040 22:05:25,921 INFO Dialect:103 - Using dialect: org.hibernate.dialect.SQLServerDialect 22:05:25,968 INFO TransactionFactoryFactory:31 - Using default transaction strategy (direct JDBC transactions) 22:05:25,968 INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended) 22:05:25,968 INFO SettingsFactory:125 - Automatic flush during beforeCompletion(): disabled 22:05:25,968 INFO SettingsFactory:129 - Automatic session close at end of transaction: disabled 22:05:25,984 INFO SettingsFactory:144 - Scrollable result sets: enabled 22:05:25,984 INFO SettingsFactory:152 - JDBC3 getGeneratedKeys(): disabled 22:05:25,984 INFO SettingsFactory:160 - Connection release mode: auto 22:05:25,984 INFO SettingsFactory:187 - Default batch fetch size: 1 22:05:26,000 INFO SettingsFactory:191 - Generate SQL with comments: disabled 22:05:26,000 INFO SettingsFactory:195 - Order SQL updates by primary key: disabled 22:05:26,000 INFO SettingsFactory:338 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory 22:05:26,046 INFO ASTQueryTranslatorFactory:24 - Using ASTQueryTranslatorFactory 22:05:26,046 INFO SettingsFactory:203 - Query language substitutions: {} 22:05:26,046 INFO SettingsFactory:209 - Second-level cache: enabled 22:05:26,046 INFO SettingsFactory:213 - Query cache: disabled 22:05:26,046 INFO SettingsFactory:325 - Cache provider: org.hibernate.cache.EhCacheProvider 22:05:26,140 INFO SettingsFactory:228 - Optimize cache for minimal puts: disabled 22:05:26,140 INFO SettingsFactory:237 - Structured second-level cache entries: disabled 22:05:26,281 INFO SettingsFactory:257 - Echoing all SQL to stdout 22:05:26,281 INFO SettingsFactory:264 - Statistics: disabled 22:05:26,281 INFO SettingsFactory:268 - Deleted entity synthetic identifier rollback: disabled 22:05:26,281 INFO SettingsFactory:283 - Default entity-mode: pojo 22:05:26,812 INFO SessionFactoryImpl:153 - building session factory 22:05:26,953 WARN Configurator:126 - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/E:/web/HibernateHome/hibernate-3.1/lib/ehcache-1.1.jar!/ehcache-failsafe.xml 22:05:28,921 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured 22:05:28,968 INFO SchemaExport:155 - Running hbm2ddl schema export 22:05:28,968 DEBUG SchemaExport:173 - import file not found: /import.sql 22:05:28,968 INFO SchemaExport:182 - exporting generated schema to database 22:05:29,156 DEBUG SchemaExport:296 - drop table myusertable 22:05:29,296 DEBUG SchemaExport:296 - create table myusertable (id int identity not null, username varchar(255) null, password varchar(255) null, email varchar(255) null, primary key (id)) 22:05:29,312 INFO SchemaExport:202 - schema export complete 22:05:29,312 WARN JDBCExceptionReporter:48 - SQL Warning: 0, SQLState: 22:05:29,312 WARN JDBCExceptionReporter:49 - \[Microsoft]\[SQLServer 2000 Driver for JDBC]Database changed to mypro 22:05:29,312 WARN JDBCExceptionReporter:48 - SQL Warning: 5701, SQLState: 01000 22:05:29,312 WARN JDBCExceptionReporter:49 - \[Microsoft]\[SQLServer 2000 Driver for JDBC]\[SQLServer]已将数据库上下文改为 'mypro'。 22:05:29,312 WARN JDBCExceptionReporter:48 - SQL Warning: 0, SQLState: 22:05:29,328 WARN JDBCExceptionReporter:49 - \[Microsoft]\[SQLServer 2000 Driver for JDBC]Language changed to 简体中文 22:05:29,328 WARN JDBCExceptionReporter:48 - SQL Warning: 5703, SQLState: 01000 22:05:29,328 WARN JDBCExceptionReporter:49 - \[Microsoft]\[SQLServer 2000 Driver for JDBC]\[SQLServer]已将语言设置改为 简体中文。 Hibernate: select user0\_.id as id0\_, user0\_.username as username0\_, user0\_.password as password0\_, user0\_.email as email0_ from myusertable user0_ 刚开始，我看日志发现并没有提示有error的出现，我又在表中添加了一条数据，然后我将hibernate show出的sql语句放入到查询分析器中，运行能够查到数据，但是一运行myeclipse后，表中的数据就没了，弄的真是崩溃了，没办法，只好一句一句的看日志，结果这句给了我一点提示: 22:05:29,156 DEBUG SchemaExport:296 - drop table myusertable 22:05:29,296 DEBUG SchemaExport:296 - create table myusertable (id int identity not null, username varchar(255) null, password varchar(255) null, email varchar(255) null, primary key (id)) 数据库对应的表进行了删除和重新的创建，难怪数据会没有，这样我想到了我的hibernate.cfg.xml文件，打开后查看代码： &lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;hibernate-configuration&gt;&lt;session-factory&gt;&lt;property name="connection.driver\_class"&gt;com.microsoft.jdbc.sqlserver.SQLServerDriver&lt;/property&gt;&lt;property name="connection.url"&gt;jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mypro&lt;/property&gt;&lt;property name="connection.username"&gt;sa&lt;/property&gt;&lt;property name="connection.password"&gt;sa&lt;/property&gt;&lt;property name="show\_sql"&gt;true&lt;/property&gt;&lt;property name="dialect"&gt;org.hibernate.dialect.SQLServerDialect &lt;/property&gt;&lt;property name="hbm2ddl.auto"&gt;create&lt;/property&gt;&lt;mapping resource="ch3/User.hbm.xml"&gt;&lt;/mapping&gt;&lt;/session-factory&gt;&lt;/hibernate-configuration&gt; 问题就出在&lt;property name="hbm2ddl.auto"&gt;create&lt;/property&gt;这句上，这句话的意思是每次启动都要进行删除表以及再次创建新表，当删除这句话后，终于不会出现这个尴尬的事情了，日志文件对于查询错误真是帮助不小呢！