Eclipse中的org.hibernate.HibernateException错误,使用Hibernate。!@!

时间:2018-01-30 04:52:53

标签: java eclipse hibernate

我正在制作一个简单的hibernate程序。 我得到了这个错误。 我正在使用Mac和Eclipse Framework。

    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:258)
    at driver.main(driver.java:12)
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1]
Message: Premature end of file.
    at java.xml/com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:652)
    at java.xml/com.sun.xml.internal.stream.XMLEventReaderImpl.peek(XMLEventReaderImpl.java:276)
    at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:103)
    ... 5 more

从driver.java,我希望使用Hibernate对数据库进行更改。我不知道错误。

我在driver.java中使用了employee类的对象。

这是employee.java

public class employee
{   

private String name;

private int Eid;

private String city;

employee(){}


//getters
public String getName()
{
    return this.name;
}
public int getEid()
{
    return this.Eid;
}
public String getCity()
{
    return this.city;
}


//setters
public void setEid(int a)
{
    this.Eid=a;
} 
public void setName(String s)
{
    this.name=s;
}
public void setCity(String s)
{
    this.city=s;
}
}

Driver .java

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class driver {

public static void main(String[] args)
{
    Configuration conf = new Configuration();
    conf.configure("/Users/ronin/Desktop/Hibernate/employee.hbm.xml");
    SessionFactory sf = conf.buildSessionFactory();
    Session s = sf.openSession();
    Transaction tx = s.beginTransaction();
    employee e = new employee();
    e.setEid(34);
    e.setName("Robin");
    e.setCity("Patiala");
    s.persist(e);
    tx.commit();
    s.close();
}
}

employee.hbm.xml文件

    <?xml version = "1.0" encoding = "utf-8"?>


<hibernate-mapping>
   <class name = "employee" table = "employee">



      <id name = "id" type = "int" column = "id">
         <generator class="native"/>
      </id>

      <property name = "name" column = "name" type = "string"/>
      <property name = "city" column = "city" type = "string"/>
      <property name = "Eid" column = "id" type = "int"/>

   </class>
</hibernate-mapping>

hibernate,cfg.xml文件

<?xml version='1.0' encoding='UTF-8'?>   

<hibernate-configuration>  

    <session-factory>  
        <property name="hbm2ddl.auto">update</property>  
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>  
        <property name="connection.url">jdbc:mysql://localhost:3306</property>  
        <property name="connection.username">root</property>  
        <property name="connection.password">admin</property>  
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
    <mapping resource="employee.hbm.xml"/>  
    </session-factory>  

</hibernate-configuration>

1 个答案:

答案 0 :(得分:1)

作为SE的一部分,JDK 9中没有JAXB。我想它是在版本9中移动J2EE。请参阅here

要在运行时使JAXB API可用,您可以添加以下命令行选项

--add-modules java.xml.bind

或作为永久性解决方案,您可以使用maven类似的方式导入这些依赖项,

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.2.8</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-core</artifactId>
    <version>2.2.8</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.2.8</version>
</dependency>

注意: 在Java 6的情况下使用JAX-B版本2.0。 在Java 7的情况下使用JAX-B版本2.2.3。 在Java 8的情况下使用JAX-B版本2.2.8