hibernate.dialect' must be set when no Connection available 使用Hibernate创建数据库表
解决方法:
创建Configuration的代码由:
Configuration config = new Configuration();
改为:
Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 数据库连接设置 --> <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="hibernate.connection.url">jdbc:oracle:thin:@192.168.1.88:1521:ora10</property> <property name="hibernate.connection.username"></property> <property name="hibernate.connection.password"></property> <!-- SQL 方言 --> <property name="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</property> <!-- 输出SQL语句 --> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <!-- 当*.hbm.xml映射文件发生变化后,在启动系统时更新数据库 --> <property name="hbm2ddl.auto">update</property> <!-- --> <mapping resource="com/entity/xxx.hbm.xml"/> <mapping resource="com/entity/xx2.hbm.xml"/> </session-factory> </hibernate-configuration>
package com.hibernate.tools; import java.net.MalformedURLException; import java.net.URL; import java.util.List; import org.hibernate.SQLQuery; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaUpdate; /** * 根据Hibernate的配置文件,生成数据库表. */ public class Hbm2Ddl { public static void main(String[] args) throws MalformedURLException { URL pathUrl = Hbm2Ddl.class.getResource(""); pathUrl = new URL(pathUrl,"hibernate.cfg.xml"); //System.out.println(pathUrl.getPath()); Configuration cfg = new Configuration().configure(pathUrl); //使用配置文件,来创建数据库表 //SchemaExport export = new SchemaExport(cfg); //export.create(true, true); //使用配置文件,来更新数据库表 SchemaUpdate update = new SchemaUpdate(cfg); update.execute(true, true); SessionFactory sf = cfg.buildSessionFactory(); Session s = sf.openSession(); SQLQuery q= s.createSQLQuery("SELECT * FROM abc"); List l = q.list();//查询出所有字段 System.out.println(l); } }

浙公网安备 33010602011771号