如何将 Hibernate 与 MySQL 连接

Posted

技术标签:

【中文标题】如何将 Hibernate 与 MySQL 连接【英文标题】:How to connect Hibernate with MySQL 【发布时间】:2018-09-30 06:15:55 【问题描述】:

我想将 Hibernate 与 mysql 连接起来。我尝试了所有方法,但我仍然收到此错误。我正在使用 Hibernate 版本。

avr. 19, 2018 1:55:21 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core 5.2.16.Final
avr. 19, 2018 1:55:21 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
avr. 19, 2018 1:55:22 PM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-mapping. Use namespace http://www.hibernate.org/dtd/hibernate-mapping instead.  Support for obsolete DTD/XSD namespaces may be removed at any time.
avr. 19, 2018 1:55:24 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations 5.0.1.Final
avr. 19, 2018 1:55:24 PM org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator initiateService
WARN: HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections
avr. 19, 2018 1:55:24 PM org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator initiateService
WARN: HHH000342: Could not obtain connection to query metadata : The application must supply JDBC connections
Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:271)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:233)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:242)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
    at TestHibernate1.main(TestHibernate1.java:14)
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:100)
    at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:54)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:137)
    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:259)

================================================ ==============================

=> 这是我的 hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost/bd_per</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password"></property>
    <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
    <property name="show_sql">true</property>
    <mapping resource="Personnes.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

=> 这是我的休眠映射

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping"><hibernate-mapping>
  <class name="Personnes" table="personnes">
    <id name="idPersonne" type="int" column="idpersonne">
      <generator class="native"/>
    </id>
    <property name="nomPersonne" type="string" not-null="true" />
    <property name="prenomPersonne" type="string" not-null="true" />


  </class>
</hibernate-mapping>

=> 最后我的类人

public class Personnes 

  private Integer idPersonne;
  private String nomPersonne;
  private String prenomPersonne;


  public Personnes(String nomPersonne, String prenomPersonne) 
    this.nomPersonne = nomPersonne;
    this.prenomPersonne = prenomPersonne;

  

  public Personnes() 
  


  public Integer getIdPersonne() 
    return idPersonne;
  

  public String getNomPersonne() 
    return nomPersonne;
  

  public String getPrenomPersonne() 
    return prenomPersonne;
  



  public void setIdPersonne(Integer integer) 
    idPersonne = integer;
  

 And,thanks for responding as quickly as possible.

  public void setPrenomPersonne(String string) 
    prenomPersonne = string;
  

public void setNomPersonne(String string) 
    // TODO Auto-generated method stub




【问题讨论】:

你有没有在你的类路径中添加mysql jar 文件? 是的,我添加了 mysql 连接器; 【参考方案1】:

至少你需要使用来自这个包https://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/dialect/package-summary.html的方言而不是来自net.sf.hibernate.dialect。例如 org.hibernate.dialect.MySQL5Dialect 您使用的包是旧的休眠版本。

为什么要使用 xml 风格的定义?

【讨论】:

我之所以使用它是因为 Hibernate(ORM) 它对我来说是新事物,我刚刚学习了一些教程,对此我不太了解。我现在应该怎么做才能解决这个问题? 将方言更改为正确的。而且我认为您需要类似本教程的内容:docs.jboss.org/hibernate/orm/3.6/quickstart/en-US/html/… 或者,这样更好,例如:spring.io/guides/gs/accessing-data-jpa【参考方案2】:

需要根据MySQL的版本选择合适的hibernate dialect。我遇到了类似的问题,使用InnoDB dialect versionMySQL 的解决方法非常简单。 您可以从包中引用正确的方言:org.hibernate.dialect

参考:Link

【讨论】:

以上是关于如何将 Hibernate 与 MySQL 连接的主要内容,如果未能解决你的问题,请参考以下文章

将 PostgreSQL 9.2.1 与 Hibernate 连接起来

MySQL/Hibernate - 我如何调试不断下降的 MySQL 池连接?

Tomcat/Hibernate 与 MySql 的连接失败,出现“通信链接失败”和“权限被拒绝”

如何将android与php mysql连接[重复]

如何将表与中间表和附加相关列spring hibernate mvc连接起来

Hibernate连接mysql数据库并自动创建表