浏览 6426 次
|
锁定老贴子 主题:为什么我的表被删除了?
该帖已经被评为精华帖
|
|
|---|---|
| 作者 | 正文 |
|
最后更新时间:2003-09-16
session建立的时候会删除数据库中的表么?(初学不懂)
================程序============= [code:1] Configuration cfg = new Configuration().addClass(Bzqj.class); SessionFactory sessions = cfg.buildSessionFactory(); Session s = sessions.openSession(); Query q = s.createQuery("from Bzqj"); for (Iterator it = q.iterate();it.hasNext(); ){ Bzqj b = (Bzqj)it.next(); System.out.println("##JGBH:"+b.getJGBH()); }[/code:1]执行后,数据库中原来的表没了,怎么回事? 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
最后更新时间:2003-09-16
肯定不是Hibernate的问题,Hibernate还不具备向数据库发送DDL语句的功能耶
|
|
| 返回顶楼 | |
|
最后更新时间:2003-09-16
test.java
[code:1] package hibernatedemo; import hibernatedemo.person; import java.util.*; import net.sf.hibernate.Query; import net.sf.hibernate.Session; import net.sf.hibernate.SessionFactory; import net.sf.hibernate.cfg.Configuration; import net.sf.hibernate.tool.hbm2ddl.SchemaExport; public class Test { public static void main(String[] args) throws Exception { Configuration cfg = new Configuration().addClass(person.class); SessionFactory sessions = cfg.buildSessionFactory(); new SchemaExport(cfg).create(true, true); Session s = sessions.openSession(); Query q = s.createQuery("from person"); for (Iterator it = q.iterate();it.hasNext();){ person b = (person)it.next(); System.out.println("##name:"+b.getName()); } } } [/code:1] person.java [code:1]package hibernatedemo; public class person { private String name; private String address; private String id; public person(){ } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }[/code:1] person.hbm.xml [code:1]<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="hibernatedemo.person" > <id name="id" column="id" type="java.lang.String"> <generator class="assigned"/> </id> <property name="name" type="java.lang.String" column="name"/> <property name="address" type="java.lang.String" column="pass"/> </class> </hibernate-mapping> [/code:1] 数据库是mysql,服务器tomcat 执行的时候控制台信息 [code:1] 2003-9-16 10:36:12 net.sf.hibernate.cfg.Environment <clinit> 信息: Hibernate 2.1 beta 3 2003-9-16 10:36:12 net.sf.hibernate.cfg.Environment <clinit> 信息: loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=com.mysql.jdbc.Driver, hibernate.cglib.use_reflection_optimizer=true, hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect, hibernate.jdbc.use_streams_for_binary=true, hibernate.jdbc.batch_size=0, hibernate.query.substitutions=true 1, false 0, yes 'Y', no 'N', hibernate.query.imports=net.sf.hibernate.test, net.sf.hibernate.eg, hibernate.connection.username=root, hibernate.connection.url=jdbc:mysql://localhost/hibernatedb, hibernate.connection.password=, hibernate.statement_cache.size=25, hibernate.connection.pool_size=1} 2003-9-16 10:36:12 net.sf.hibernate.cfg.Environment <clinit> 信息: using java.io streams to persist binary types 2003-9-16 10:36:12 net.sf.hibernate.cfg.Environment <clinit> 信息: using CGLIB reflection optimizer 2003-9-16 10:36:12 net.sf.hibernate.cfg.Configuration addClass 信息: Mapping resource: hibernatedemo/person.hbm.xml 2003-9-16 10:36:15 net.sf.hibernate.cfg.Binder bindRootClass 信息: Mapping class: hibernatedemo.person -> person 2003-9-16 10:36:15 net.sf.hibernate.cfg.Configuration secondPassCompile 信息: processing one-to-many association mappings 2003-9-16 10:36:15 net.sf.hibernate.cfg.Configuration secondPassCompile 信息: processing one-to-one association property references 2003-9-16 10:36:15 net.sf.hibernate.cfg.Configuration secondPassCompile 信息: processing foreign key constraints 2003-9-16 10:36:15 net.sf.hibernate.dialect.Dialect <init> 信息: Using dialect: net.sf.hibernate.dialect.MySQLDialect 2003-9-16 10:36:15 net.sf.hibernate.cfg.SettingsFactory buildSettings 信息: Use outer join fetching: true 2003-9-16 10:36:15 net.sf.hibernate.connection.DriverManagerConnectionProvider configure 信息: Using Hibernate built-in connection pool (not for production use!) 2003-9-16 10:36:15 net.sf.hibernate.connection.DriverManagerConnectionProvider configure 信息: Hibernate connection pool size: 1 2003-9-16 10:36:15 net.sf.hibernate.connection.DriverManagerConnectionProvider configure 信息: using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost/hibernatedb 2003-9-16 10:36:15 net.sf.hibernate.connection.DriverManagerConnectionProvider configure 信息: connection properties: {user=root, password=} 2003-9-16 10:36:15 net.sf.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup 信息: No TransactionManagerLookup configured (use of process level read-write cache is not recommended) 2003-9-16 10:36:16 net.sf.hibernate.cfg.SettingsFactory buildSettings 信息: Use scrollable result sets: true 2003-9-16 10:36:16 net.sf.hibernate.cfg.SettingsFactory buildSettings 信息: Query language substitutions: {no='N', true=1, yes='Y', false=0} 2003-9-16 10:36:16 net.sf.hibernate.cfg.SettingsFactory buildSettings 信息: cache provider: net.sf.hibernate.cache.JCSCacheProvider 2003-9-16 10:36:16 net.sf.hibernate.cfg.Configuration configureCaches 信息: instantiating and configuring caches 2003-9-16 10:36:16 net.sf.hibernate.impl.SessionFactoryImpl <init> 信息: building session factory 2003-9-16 10:36:18 net.sf.hibernate.impl.SessionFactoryObjectFactory addInstance 信息: no JNDI name configured 2003-9-16 10:36:18 net.sf.hibernate.dialect.Dialect <init> 信息: Using dialect: net.sf.hibernate.dialect.MySQLDialect 2003-9-16 10:36:18 net.sf.hibernate.cfg.Configuration secondPassCompile 信息: processing one-to-many association mappings 2003-9-16 10:36:18 net.sf.hibernate.cfg.Configuration secondPassCompile 信息: processing one-to-one association property references 2003-9-16 10:36:18 net.sf.hibernate.cfg.Configuration secondPassCompile 信息: processing foreign key constraints 2003-9-16 10:36:18 net.sf.hibernate.cfg.Configuration secondPassCompile 信息: processing one-to-many association mappings 2003-9-16 10:36:18 net.sf.hibernate.cfg.Configuration secondPassCompile 信息: processing one-to-one association property references 2003-9-16 10:36:18 net.sf.hibernate.cfg.Configuration secondPassCompile 信息: processing foreign key constraints 2003-9-16 10:36:18 net.sf.hibernate.tool.hbm2ddl.SchemaExport execute 信息: Running hbm2ddl schema export 2003-9-16 10:36:18 net.sf.hibernate.tool.hbm2ddl.SchemaExport execute 信息: exporting generated schema to database 2003-9-16 10:36:18 net.sf.hibernate.connection.DriverManagerConnectionProvider configure 信息: Using Hibernate built-in connection pool (not for production use!) 2003-9-16 10:36:18 net.sf.hibernate.connection.DriverManagerConnectionProvider configure 信息: Hibernate connection pool size: 1 2003-9-16 10:36:18 net.sf.hibernate.connection.DriverManagerConnectionProvider configure 信息: using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost/hibernatedb 2003-9-16 10:36:18 net.sf.hibernate.connection.DriverManagerConnectionProvider configure 信息: connection properties: {user=root, password=} drop table person create table person ( id VARCHAR(255) not null, name VARCHAR(255), pass VARCHAR(255), primary key (id) ) 2003-9-16 10:36:18 net.sf.hibernate.tool.hbm2ddl.SchemaExport execute 信息: schema export complete 2003-9-16 10:36:18 net.sf.hibernate.connection.DriverManagerConnectionProvider close 信息: cleaning up connection pool: jdbc:mysql://localhost/hibernatedb 2003-9-16 10:36:18 net.sf.hibernate.connection.DriverManagerConnectionProvider close 信息: cleaning up connection pool: jdbc:mysql://localhost/hibernatedb [/code:1] |
|
| 返回顶楼 | |
|
最后更新时间:2003-09-16
呵呵,难怪,难怪,原来Hibernate也可以自动建表,枉我还在jdon和别人争,被别人说JDO有自动建表功能,Hibernate没有,原来也是有滴。
|
|
| 返回顶楼 | |
|
最后更新时间:2003-09-16
|
|
| 返回顶楼 | |
|
最后更新时间:2003-09-16
呵呵,其实我早就在用了, 只不过一直糊涂的认为不支持。
[code:1] SchemaExport dbExport = new SchemaExport(conf); dbExport.setOutputFile("db\\testsub.txt"); dbExport.create(true, true); [/code:1] dbExport.create(true, true); 方法中第一个参数是用来决定是否产生DDL,第二个参数是用来决定是否执行DDL |
|
| 返回顶楼 | |
|
最后更新时间:2003-09-18
关于自动建表我们通常需要这样的功能:
数据库中不存在表的话则创建,如果存在就不要先Drop再创建了。否则数据都丢失了。 这样我们的系统打包后第一次运行就可以建好表,以后就使用这些表而不再创建了。 各位有何好的方法? |
|
| 返回顶楼 | |
|
最后更新时间:2003-09-18
你可以先尝试着查询一下,如果捕获的异常说明没有表,就创建之;反之则不创建
|
|
| 返回顶楼 | |








