gpt4 book ai didi

java - Eclipse 中使用 Hibernate 时出现 org.hibernate.HibernateException 错误。!@!

转载 作者:行者123 更新时间:2023-12-02 11:41:34 24 4
gpt4 key购买 nike

我正在制作一个简单的 hibernate 程序。我收到这个错误。我为此使用了 Mac 和 Eclipse 框架。

    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.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;
}
}

驱动程序.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>

最佳答案

JAXB 在 JDK 9 中无法作为 SE 的一部分使用。我猜它已在版本 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

关于java - Eclipse 中使用 Hibernate 时出现 org.hibernate.HibernateException 错误。!@!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48514087/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com