当前位置: 首页 > 面试题库 >

配置Hibernate 5.0.1和MySQL时出错

宋建柏
2023-03-14
问题内容

我在使用MySQL配置hibernate 5.0.1时遇到问题,我在这里看到一些问题,指出版本4中存在错误,我不确定这是否是相同的错误,或者我做错了什么。

这是我的配置文件hibernate.cfg.xml

<hibernate-configuration
        xmlns="http://www.hibernate.org/xsd/hibernate-configuration"
        xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <session-factory>
    <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/hibernatedb</property>
    <property name="connection.username">root</property>
    <property name="connection.password"></property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>

    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">update</property>

    <mapping resource="org.cypher.dto.UserDetails"/>
  </session-factory>
</hibernate-configuration>

这是我的sessionfactory代码

SessionFactory sessionfactory = new Configuration().configure().buildSessionFactory();
Session session = sessionfactory.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();

这是错误日志。

Exception in thread "main" org.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line number 12 and column 63 in RESOURCE hibernate.cfg.xml. Message: cvc-elt.1: Cannot find the declaration of element 'hibernate-configuration'.
    at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:133)
    at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:65)
    at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:55)
    at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:163)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:259)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:245)
    at org.cypher.hibernate.HibernateTest.main(HibernateTest.java:15)
Caused by: javax.xml.bind.UnmarshalException

谢谢。


问题答案:

所以这是解决方案。

更换

<hibernate-configuration
        xmlns="http://www.hibernate.org/xsd/hibernate-configuration"
        xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<hibernate-configuration>

并在文件顶部添加以下代码。

<!DOCTYPE hibernate-configuration PUBLIC 
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

同样在配置文件中替换<mapping resource=...><mapping class=...>



 类似资料:
  • 我有这个例外。我换了版本,但想不通。我在哪里犯错误?Tomcat版本8.5 org.springframework.beans.factory.BeanCreationExcture:错误创建在ServletContext资源[/WEB-INF/Spring/appServlet/servlet-context.xml]中定义的名称为“会话工厂”的bean:调用init方法失败;嵌套异常java.

  • MySQL 安装 假设当前用户为:root Mysql 安装 官网:http://www.mysql.com/ 官网下载:http://dev.mysql.com/downloads/mysql/ 官网 5.5 下载:http://dev.mysql.com/downloads/mysql/5.5.html#downloads 官网 5.6 下载:http://dev.mysql.com/down

  • 我正在尝试为Windows7安装和配置apache hadoop。 我正在遵循以下教程http://blog.sqltrainer.com/2012/01/installing-and-configuring-apache.html 我用作者在上面提到的文章中提到的#export评论了一下。我得到的错误如下 /usr/local/hadoop/libexec/../conf/hadoop-env.

  • springboot 配置多源数据库问题。 测试了一下springboot配置多源数据库,mysql和postgresql,yml配置如下: 运行程序,出现如下错误: …… 9:30:59:048] [INFO] - org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.prepareWebA

  • 本文向大家介绍详解CentOS7下安装Mysql和配置mysql,包括了详解CentOS7下安装Mysql和配置mysql的使用技巧和注意事项,需要的朋友参考一下 终于在centos下安装完了MySQL,使用的是yum在线安装方式。没有用源码安装,因为在线安装省时省力。 在上一台机器中,不知道什么原因使用yum在线安装速度很慢,无法下载,于是重做了系统安装,至于什么原因导致的网速慢暂时还未解决,以

  • 在我的spring boot 2应用程序中,我有一个应用程序。yml as, 我的配置类是, 代码在本地计算机中运行良好,所有值都按预期加载。但cloud env Consor将yml文件序列化为环境变量,因此我的配置表示为, 这是一个字符串。我不知道Consor以何种格式表示/序列化值。现在我的DefaultRateLimitsConfig无法加载,因为它会出错, 如何以更干净的方式在两个环境中