簡體   English   中英

使用休眠連接數據庫時出錯

[英]Error connecting with database using hibernate

我正在嘗試使用休眠在數據庫上創建表。 這是我的代碼

Java對象

打包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;
}

數據訪問對象

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();

    }
}

這是我的hibernate.config.xml文件

> <?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>

錯誤訊息

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

請幫我整理一下。 先感謝您。

問題在於注釋XML的方式。

<!-- Database connection settings -->

嘗試這個

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

如何用XML注釋一行?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM