使用 Eclipse 和 MySql 进行 Hibernate 逆向工程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3198119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 14:52:12  来源:igfitidea点击:

Hibernate Reverse Engineering with Eclipse and MySql

mysqldatabaseeclipsehibernate

提问by sarasota

I'm having the hardest time getting Eclipse to connect and reverse engineer from a MySQL5 database. I can see Eclipse connecting to my MySQL database and can even see the tables through the "Data Source Explorer" view but when I try it after creating Hibernate Console and Configuration files, I get the error:

我最难的是让 Eclipse 从 MySQL5 数据库连接和逆向工程。我可以看到 Eclipse 连接到我的 MySQL 数据库,甚至可以通过“数据源资源管理器”视图查看表,但是当我在创建 Hibernate 控制台和配置文件后尝试它时,出现错误:

org.hibernate.console.HibernateConsoleRuntimeException:
Problems while loading database
    driverclass (com.mysql.jdbc.Driver)
Problems while loading database
    driverclass (com.mysql.jdbc.Driver)  
java.lang.ClassNotFoundException:com.mysql.jdbc.Driver
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

From my simple java project, I start by creating a Hibernate Configuration File (cfg.xml).

在我的简单 java 项目中,我首先创建了一个 Hibernate 配置文件 (cfg.xml)。

Name of file: hibernate.cfg.xml
Session Factory name: org.hibernate.SessionFactory
Database Dialect: org.hibernate.dialect.MySQL5Dialect      
Driver Class: com.mysql.jdbc.Driver      
Connection Url: jdbc:mysql://localhost:3306/<myDatabaseName>    
Default Schema: <myDatabaseName>        
Username: correct username  
Password: correct password

I also have selected the option to "Create a console Configuration"

我还选择了“创建控制台配置”选项

At this point, I can see the new configuration listed in my "Hibernate Configuration" perspective/workbench pane. If I try to expand it, I get the earlier listed error.

此时,我可以看到在“Hibernate Configuration”透视图/工作台窗格中列出的新配置。如果我尝试扩展它,我会收到前面列出的错误。

I don't understand why I can see the database through the 'Data Source Explorer" and even though I'm using the DB connection profile listed in there as part of my configuration, I still get this error.

我不明白为什么我可以通过“数据源资源管理器”查看数据库,即使我使用其中列出的数据库连接配置文件作为配置的一部分,我仍然收到此错误。

I also tried to create a new database profile using a manual mysql connector jar (mysql-connector-java-5.1.13-bin.jar) and same end result.

我还尝试使用手动 mysql 连接器 jar (mysql-connector-java-5.1.13-bin.jar) 创建一个新的数据库配置文件,最终结果相同。

Versions of what I have:

我所拥有的版本:

Eclipse version: 3.6.0 aka Helios, Build 20100617 - 1415
MySQL: 5.1.34
Hibernate Tools (from JBoss): HibernateTools-3.3.1.v201006011046R-H111-GA 
(placed into Eclipse's 'dropins' folder)

What am I doing wrong in my hibernate configuration setup?

我在休眠配置设置中做错了什么?

Help!

帮助!

回答by Pascal Thivent

Your Hibernate Console Configuration doesn't have the MySQL JDBC driver on its classpath, hence the java.lang.ClassNotFoundException:com.mysql.jdbc.Driver. From the reference guide of the Hibernate Tools:

您的 Hibernate 控制台配置在其类路径上没有 MySQL JDBC 驱动程序,因此java.lang.ClassNotFoundException:com.mysql.jdbc.Driver. 来自 Hibernate Tools 的参考指南:

3.4. Creating a Hibernate Console Configuration

...

alt text http://docs.jboss.org/tools/2.1.0.Beta1/hibernatetools/html_single/images/plugins/plugins_3.png

...

  • Classpath: The classpath for loading POJO and JDBC drivers; only needed if the default classpath of the Project does not contain the required classes. Do not add Hibernate core libraries or dependencies, they are already included. If you get ClassNotFound errors then check this list for possible missing or redundant directories/jars.
  • Include default classpath from project: When enabled the project classpath will be appended to the classpath specified above

3.4. 创建休眠控制台配置

...

替代文字 http://docs.jboss.org/tools/2.1.0.Beta1/hibernatetools/html_single/images/plugins/plugins_3.png

...

  • Classpath:加载POJO和JDBC驱动的类路径;仅当项目的默认类路径不包含所需的类时才需要。不要添加 Hibernate 核心库或依赖项,它们已经包含在内。如果您收到 ClassNotFound 错误,请检查此列表是否有可能丢失或冗余的目录/jar。
  • 包括项目的默认类路径:启用后,项目类路径将附加到上面指定的类路径

Does the "associated project" have the MySQL JDBC driver declared as library? If not, then you must add it (either as a project library or in the above tab).

“关联项目”是否将 MySQL JDBC 驱动程序声明为库?如果没有,那么您必须添加它(作为项目库或在上面的选项卡中)。