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); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架