hibernate-----异常(1)

<span style="font-size:18px;">Exception in thread "main" java.util.ServiceConfigurationError: <span style="color:#ff0000;">org.hibernate.boot.model.TypeContributor: Provider org.hibernate.type.Java8DateTimeTypeContributor not found</span>
	at java.util.ServiceLoader.fail(ServiceLoader.java:231)
	at java.util.ServiceLoader.access$300(ServiceLoader.java:181)
	at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:365)
	at java.util.ServiceLoader$1.next(ServiceLoader.java:445)
	at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.loadJavaServices(ClassLoaderServiceImpl.java:324)
	at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:356)
	at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
	at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
	at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
	at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
	at StudentTest.main(StudentTest.java:17)</span>

StudentTest.java

<span style="font-size:18px;">import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.dqpi.eonline.model.Student;


public class StudentTest {
	public static void main(String[] args) {
		Student s = new Student();
		s.setId(1);
		s.setName("s1");
		s.setAge(18);
		//1.获取Configuration对象
		Configuration configuration = new Configuration();
		//2.通过configuration来获取SessionFactory对象
		SessionFactory sessionFactory =  configuration.configure().buildSessionFactory();
		//3.通过SessionFactory来获取session对象
		Session session = sessionFactory.openSession();
		//开启事务保存session
		session.beginTransaction();
		//把student对象s保存到数据库中
		session.save(s);
		//提交事务
		session.getTransaction().commit();
		//关闭session
		session.close();
		//关闭sessionFactory
		sessionFactory.close();
	}
}
</span>
Student.java

<span style="font-size:18px;">package com.dqpi.eonline.model;

public class Student {
	private int id;
	private String name;
	private int age;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
}
</span>
hibernate.cfg.xml

<span style="font-size:18px;"><?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="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>

        <!-- 数据库连接池 -->
       <!--  <property name="connection.pool_size">1</property> -->

        <!--数据库方言-->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <!-- <property name="current_session_context_class">thread</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 and re-create the database schema on startup -->
        <!-- <property name="hbm2ddl.auto">update</property> -->
		
		<!--做实体类与mysql数据库之间的映射  -->
        <mapping resource="com/dqpi/eonline/model/Student.hbm.xml"/>

    </session-factory>

</hibernate-configuration></span>
Student.hbm.xml

<span style="font-size:18px;"><?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<<span style="color:#ff0000;">!--通过写student.hbm.xml来做实体类与hiernate之间的映射,在hibernate.cfg.xml中<mapping resource="">来做配置映射  -->
<!--此处的package对应的是实体类所对应的包名,做映射  --></span>
<hibernate-mapping package="com.dqpi.eonline.model">
	<span style="color:#ff0000;"><!--class中配置实体的类名和数据库表的名字,如果表名和实体类名一样,则数据库表的名字可以省略不写  --></span>
	<class name="Student" table="student">
		<span style="color:#ff0000;"><!--id是用来指定数据库的主键,name中的值是实体类的属性,column是数据库表中字段名字,如果一致,则可以省略不写  --></span>
		<id name="id" column="id"></id>
		<span style="color:#ff0000;"><!--property指的是数据库表中除主键外其他的字段和实体类中的其余属性  --></span>
		<property name="name"></property>
		<property name="age"></property>
	</class>

</hibernate-mapping></span>

上面是异常的信息,可以看出来是: Provider org.hibernate.type.Java8DateTimeTypeContributor not found找不到

解决方案:先看看你的项目jar中有没有hibernate-java8...那个jar,如果没有就需要添加那个jar包,如果有的话就检查一下你的JDK(一般JDK和JRE是统一的版本),我就是因为使用的是JDK1.7,所以才会报这个异常,然后我下载了JDK1.8,在myeclipse中重新配置了JDK,并且把原来项目中的JRE  System Library换成了1.8的,然后在运行就好使了,没有出这个错误,这个问题归根到底就是JDK的版本问题,因为每个JDK的不同版本中都或多或少有些不同,而框架的最新包中也会更新,所以要注意这一点。

注意:我用的hibernate版本是5.0.7Final版本

你可能感兴趣的:(Hibernate,exception)