使用hibernate连接数据库时出错 [英] Error connecting with database using hibernate

查看:116
本文介绍了使用hibernate连接数据库时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用hibernate在我的数据库上创建一个表。这里是我的代码



java对象



package com.digitek.students;

  import javax.persistence.Entity; 
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name =studentInfo)
public class StudentInfo {

@Id
private int rollNo;
public int getRollNo(){
return rollNo;
}
public void setRollNo(int rollNo){
this.rollNo = rollNo;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
私人字符串名称;
}

数据访问对象

  package com.digitek.students; 

import org.hibernate.Session;
import org.hibernate.SessionFactory;
导入org.hibernate.cfg.Configuration;

public class Main {

public static void main(String [] args){

StudentInfo student = new StudentInfo();
student.setName(Rishit);
student.setRollNo(47);

SessionFactory sf = new Configuration()。configure()。buildSessionFactory();
会话会话= sf.openSession();
session.beginTransaction();
session.save(student);
session.getTransaction()。commit();
session.close();
sf.close();





这里是我的hibernate.config。 xml文件


 > <?xml version ='1.0'encoding ='utf-8'?> <! - 〜Hibernate,关系
>习惯Java的持久性~~许可证:GNU Lesser General
>公共许可证(LGPL),版本2.1或更高版本。 〜查看lgpl.txt文件
>在根目录或< http://www.gnu.org/licenses/lgpl-2.1.html> ;.
> - > <!DOCTYPE hibernate-configuration PUBLIC
> - // Hibernate / Hibernate Configuration DTD 3.0 // EN
> http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd\">
>
>冬眠-结构>
>
> <会话工厂>
>
> <! - 数据库连接设置 - >
> <! - < property name =connection.driver_class> org.hsqldb.jdbcDriver< / property>
> < property name =connection.url> jdbc:hsqldb:hsql:// localhost / TestDB< / property>
> - >
>
> < property name =connection.driver_class> org.mysql.Driver< / property>
> < property name =connection.url> jdbc:mysql:// localhost:3306 / hibernatetutorials< / property>
> < property name =connection.username> root< / property>
> < property name =connection.password>< / property>
>
> <! - JDBC连接池(使用内置) - >
> < property name =connection.pool_size> 1< / property>
>
> <! - SQL方言 - >
> < property name =dialect>
> org.hibernate.dialect.MySQLDialect
> < /性>
>
> <! - 启用Hibernate的自动会话上下文管理 - >
> < property name =current_session_context_class> thread< / property>
>
> < property name =cache.use_query_cache> true< / property>
> < property name =cache.use_second_level_cache> true< / property>
> < property name =cache.use_structured_entries> true< / property>
> < property name =cache.region.factory_class> org.hibernate.cache.EhCacheRegionFactory< / property>
> < property name =net.sf.ehcache.configurationResourceName> /hibernate-config/ehcache.xml< / property>
>
> <! - 禁用第二级缓存 - >
> < property name =cache.provider_class> org.hibernate.cache.NoCacheProvider< / property>
>
> <! - 将所有执行的SQL回复到stdout - >
> < property name =show_sql> true< / property>
>
> <! - 删除所有现有的表格并创建新的表格 - >
> < property name =hbm2ddl.auto>建立< / property>
>
> < mapping class =com.digitek.students.StudentInfo/>
>
> < /会话工厂>
>
> < /休眠-结构>

错误讯息

  2015年11月2日上午11点02分26秒org.hibernate.Version logVersion 
信息:HHH000412:Hibernate Core {5.0.2.Final}
Nov 02,2015 11:02 :26 AM org.hibernate.cfg.Environment< clinit>
信息:HHH000206:找不到hibernate.properties
2015年11月02日11:02:26 org.hibernate.cfg.Environment buildBytecodeProvider
INFO:HHH000021:字节码提供程序名称:javassist
Nov 02,2015 11:02:26 AM org.hibernate.boot.jaxb.in​​ternal.stax.LocalXmlResourceResolver resolveEntity
WARN:HHH90000012:识别过时的hibernate命名空间http://hibernate.sourceforge.net/hibernate-组态。改用名称空间http://www.hibernate.org/dtd/hibernate-configuration。随时可以删除对过时的DTD / XSD名称空间的支持。
线程main中的异常org.hibernate.HibernateException:在org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:107)
处访问stax流
时出错org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:65)
at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:57)
at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:163)
在org.hibernate.cfg.Configuration.configure(Configuration.java:259)
在org.hibernate.cfg。 Configuration.configure(Configuration.java:245)
at com.digitek.students.Main.main(Main.java:15)
引起:javax.xml.stream.XMLStreamException:ParseError at [row ,col]:[12,1]
消息:prolog中不允许使用内容。
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source)
at com.sun.xml.internal.stream.XMLEventReaderImpl.peek(Unknown Source)
at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:103)
... 6 more

请帮我解决这个问题。

解决方案

问题在于XML被评论的方式。

 <! - 数据库连接设置 - > 

试试这个

 <! -  
<数据库连接设置>
- > b


$ b

comment-a-single-line-in-xml>如何评论XML中的单行?


I am trying to create a table on my database using hibernate. Here is my code

The java object

package com.digitek.students;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="studentInfo")
public class StudentInfo {

    @Id
    private int rollNo;
    public int getRollNo() {
        return rollNo;
    }
    public void setRollNo(int rollNo) {
        this.rollNo = rollNo;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    private String name;
}

The data access object

package com.digitek.students;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Main {

    public static void main(String[] args){

        StudentInfo student = new StudentInfo();
        student.setName("Rishit");
        student.setRollNo(47);

        SessionFactory sf = new Configuration().configure().buildSessionFactory(); 
        Session session = sf.openSession();
        session.beginTransaction();
        session.save(student);
        session.getTransaction().commit();
        session.close();
        sf.close();

    }
}

Here is my hibernate.config.xml file

> <?xml version='1.0' encoding='utf-8'?> <!--   ~ Hibernate, Relational
> Persistence for Idiomatic Java   ~   ~ License: GNU Lesser General
> Public License (LGPL), version 2.1 or later.   ~ See the lgpl.txt file
> in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. 
> --> <!DOCTYPE hibernate-configuration PUBLIC
>         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
>         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
> 
> hibernate-configuration>
> 
>     <session-factory>
> 
>         <!-- Database connection settings -->
>         <!-- <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
>                     <property name="connection.url">jdbc:hsqldb:hsql://localhost/TestDB</property>
> -->
> 
>         <property name="connection.driver_class">org.mysql.Driver</property>
>         <property name="connection.url">jdbc:mysql://localhost:3306/hibernatetutorials</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>
> 
>         <property name="cache.use_query_cache">true</property>
>         <property name="cache.use_second_level_cache">true</property>
>         <property name="cache.use_structured_entries">true</property>
>         <property name="cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</property>
>         <property name="net.sf.ehcache.configurationResourceName">/hibernate-config/ehcache.xml</property>
>         
>         <!-- Disable the second-level cache  -->
>         <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>        
>         
>        <!-- Echo all executed SQL to stdout -->
>         <property name="show_sql">true</property>
>         
>         <!-- Drop all existing tables and create new ones -->
>         <property name="hbm2ddl.auto">create</property>
> 
>         <mapping class="com.digitek.students.StudentInfo"/>
> 
>     </session-factory>
> 
> </hibernate-configuration>

The error message

Nov 02, 2015 11:02:26 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.0.2.Final}
Nov 02, 2015 11:02:26 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Nov 02, 2015 11:02:26 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Nov 02, 2015 11:02:26 AM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead.  Support for obsolete DTD/XSD namespaces may be removed at any time.
Exception in thread "main" org.hibernate.HibernateException: Error accessing stax stream
    at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:107)
    at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:65)
    at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:57)
    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 com.digitek.students.Main.main(Main.java:15)
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[12,1]
Message: Content is not allowed in prolog.
    at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source)
    at com.sun.xml.internal.stream.XMLEventReaderImpl.peek(Unknown Source)
    at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:103)
    ... 6 more

Please help me sort this. Thank you in advance.

解决方案

The problem is with the way XML is commented.

<!-- Database connection settings -->

Try this

<!--
<Database connection settings>
-->

How to comment a single line in XML?

这篇关于使用hibernate连接数据库时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆