使用Hibernate时未找到类定义错误
这是我第一次尝试用XML的方式使用Hibernate框架,但没有明显的原因,我遇到了以下异常 NoClassDefFoundError:javax/xml/bind/JAXBException 这是一个Maven项目,我已将所有xml文件放在resources目录中。我的配置文件有问题吗使用Hibernate时未找到类定义错误,hibernate,Hibernate,这是我第一次尝试用XML的方式使用Hibernate框架,但没有明显的原因,我遇到了以下异常 NoClassDefFoundError:javax/xml/bind/JAXBException 这是一个Maven项目,我已将所有xml文件放在resources目录中。我的配置文件有问题吗 hibernate.cfg.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBL
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<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/mydb</property>
<property name="connection.username">me</property>
<property name="connection.password"></property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<mapping resource="order.hbm.xml"/>
</session-factory>
</hibernate-configuration>
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.company.Entities.Order" table="order">
<id name="id" type = "int" column = "oid">
<generator class="native"/>
</id>
<property name="name" column="name" type="string" />
</class>
</hibernate-mapping>
你说你使用JDK9。自版本9以来,jaxb不再出现在这个JDK中。您必须在依赖项上设置它,例如:
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
javax.xml.bind
jaxb api
2.3.0
com.sun.xml.bind
jaxb内核
2.3.0
com.sun.xml.bind
jaxb impl
2.3.0
您使用哪个JDK版本?您是否使用像maven这样的依赖项管理工具?@wargre我使用JDK 9版本。这是Maven项目,我在问题中已经提到过。我添加了这些依赖项。我得到了一个不同的错误。“org.hibernate.MappingException:未知实体”这是另一个错误。无论如何,只需使用以下内容构建会话工厂:returnnewconfiguration().configure(“hibernate.cfg.xml”).buildSessionFactory()代码>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>