这是我第一次尝试以XML方式使用Hibernate Framework,并且由于没有明显的原因而遇到以下异常。
java.lang.NoClassDefFoundError:javax / xml / bind / JAXBException
这是一个Maven项目,我已将所有xml文件放置在资源目录中。我的配置文件有问题吗?
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>
order.hbm.xml
<?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>
主要
public class MainApplication
{
static Session sessionObj;
static SessionFactory sessionFactoryObj;
private static SessionFactory buildSessionFactory()
{
Configuration configObj = new Configuration();
configObj.configure("hibernate.cfg.xml");
ServiceRegistry serviceRegistryObj = new StandardServiceRegistryBuilder().applySettings(configObj.getProperties()).build();
sessionFactoryObj = configObj.buildSessionFactory(serviceRegistryObj);
return sessionFactoryObj;
}
public static void main(String[] args)
{
Order o = new Order();
//o.setId(1);
o.setName("first order name");
sessionObj = buildSessionFactory().openSession();
sessionObj.beginTransaction();
sessionObj.save(o);
sessionObj.getTransaction().commit();
if(sessionObj != null)
{
sessionObj.close();
}
}
}
答案 0 :(得分:1)
您说您使用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>