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);
        
        
    }
}
复制代码

 

posted @   letmedown  阅读(821)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示