1. 首先创建\META-INF\persistence.xml配置文件并做简单的配置

    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
                version="2.0">

       <persistence-unit name="org.hibernate.tutorial.em">
           <description>
              Persistence unit for the JPA tutorial of the Hibernate Getting Started Guide
           </description>

           <properties>
               <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
               <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/databaseName?useSSL=false&amp;serverTimezone=UTC&amp;verifyServerCertifate=false&amp;allowPublicKeyRetrieval=true" />
               <property name="javax.persistence.jdbc.user" value="root" />
               <property name="javax.persistence.jdbc.password" value="passwd" />
    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect"/>
               <property name="hibernate.show_sql" value="true" />
               <property name="hibernate.format_sql" value="true"/>
               <property name="hibernate.hbm2ddl.auto" value="create" />
           </properties>

       </persistence-unit>

    </persistence>
  2. 创建实体Java类

    import java.util.Date;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;
    import javax.persistence.Temporal;
    import javax.persistence.TemporalType;

    import org.hibernate.annotations.GenericGenerator;

    @Entity
    @Table( name = "EVENTS" )
    public class Event {
       private Long id;

       private String title;
       private Date date;

       public Event() {
           // this form used by Hibernate
      }

       public Event(String title, Date date) {
           // for application use, to create new events
           this.title = title;
           this.date = date;
      }

       @Id
       @GeneratedValue(generator="increment")
       @GenericGenerator(name="increment", strategy = "increment")
       public Long getId() {
           return id;
      }

       private void setId(Long id) {
           this.id = id;
      }

       @Temporal(TemporalType.TIMESTAMP)
       @Column(name = "EVENT_DATE")
       public Date getDate() {
           return date;
      }

       public void setDate(Date date) {
           this.date = date;
      }

       public String getTitle() {
           return title;
      }

       public void setTitle(String title) {
           this.title = title;
      }
    }
  3. 向META-INF/persistence.xml文件中添加映射信息

    <class>类路径.Event</class>
  4. JUnit测试测试程序

    import java.util.Date;
    import java.util.List;
    import javax.persistence.EntityManager;
    import javax.persistence.EntityManagerFactory;
    import javax.persistence.Persistence;

    import junit.framework.TestCase;

    /**
    * Illustrates basic use of Hibernate as a JPA provider.
    *
    * @author Steve Ebersole
    */
    public class EntityManagerIllustrationTest extends TestCase {
       private EntityManagerFactory entityManagerFactory;

       @Override
       protected void setUp() throws Exception {
           // like discussed with regards to SessionFactory, an EntityManagerFactory is set up once for an application
           // IMPORTANT: notice how the name here matches the name we gave the persistence-unit in persistence.xml!
           entityManagerFactory = Persistence.createEntityManagerFactory( "org.hibernate.tutorial.em" );
      }

       @Override
       protected void tearDown() throws Exception {
           entityManagerFactory.close();
      }

       public void testBasicUsage() {
           // create a couple of events...
           EntityManager entityManager = entityManagerFactory.createEntityManager();
           entityManager.getTransaction().begin();
           entityManager.persist( new Event( "Our very first event!", new Date() ) );
           entityManager.persist( new Event( "A follow up event", new Date() ) );
           entityManager.getTransaction().commit();
           entityManager.close();

           // now lets pull events from the database and list them
           entityManager = entityManagerFactory.createEntityManager();
           entityManager.getTransaction().begin();
           List<Event> result = entityManager.createQuery( "from Event", Event.class ).getResultList();
           for ( Event event : result ) {
               System.out.println( "Event (" + event.getDate() + ") : " + event.getTitle() );
          }
           entityManager.getTransaction().commit();
           entityManager.close();
      }
    }
  5. 运行测试

Hibernate 5 入门指南-基于JPA的更多相关文章

  1. Hibernate 5 入门指南-基于映射文件

    由于Hibernate 4版本混乱,Hibernate 3有些过时,Hibernate 5的开发文档尚不完善,所以构建一份简单的Hibernate 5的入门指南 注:案例参考Hibernate 官方参 ...

  2. Hibernate 5 入门指南-基于Envers

    首先创建\META-INF\persistence.xml配置文件并做简单的配置 <persistence xmlns="http://java.sun.com/xml/ns/pers ...

  3. Hibernate 5 入门指南-基于类注解

    首先创建hibernate.cfg.xml配置文件并做简单的配置 <hibernate-configuration>    <session-factory>        & ...

  4. JPA入门例子(采用JPA的hibernate实现版本) 转

    JPA入门例子(采用JPA的hibernate实现版本) jpahibernate数据库jdbcjava框架(1).JPA介绍: JPA全称为Java Persistence API ,Java持久化 ...

  5. JPA入门例子(采用JPA的hibernate实现版本) --- 会伴随 配置文件:persistence.xml

    JPA入门例子(采用JPA的hibernate实现版本) 分类: j2se2011-03-30 16:09 45838人阅读 评论(9) 收藏 举报 jpahibernate数据库jdbcjava框架 ...

  6. JPA入门例子(采用JPA的hibernate实现版本)

    (1).JPA介绍: JPA全称为Java Persistence API ,Java持久化API是Sun公司在Java EE 5规范中提出的Java持久化接口.JPA吸取了目前Java持久化技术的优 ...

  7. 张高兴的 .NET IoT 入门指南:(八)基于 GPS 的 NTP 时间同步服务器

    时间究竟是什么?这既可以是一个哲学问题,也可以是一个物理问题.古人对太阳进行观测,利用太阳的投影发明了日晷,定义了最初的时间.随着科技的发展,天文观测的精度也越来越准确,人们发现地球的自转并不是完全一 ...

  8. Vue.js 入门指南之“前传”(含sublime text 3 配置)

    题记:关注Vue.js 很久了,但就是没有动手写过一行代码,今天准备入手,却发现自己比菜鸟还菜,于是四方寻找大牛指点,才终于找到了入门的“入门”,就算是“入门指南”的“前传”吧.此文献给跟我一样“白痴 ...

  9. 【翻译】Fluent NHibernate介绍和入门指南

    英文原文地址:https://github.com/jagregory/fluent-nhibernate/wiki/Getting-started 翻译原文地址:http://www.cnblogs ...

随机推荐

  1. netty源码解解析(4.0)-14 Channel NIO实现:读取数据

     本章分析Nio Channel的数据读取功能的实现. Channel读取数据需要Channel和ChannelHandler配合使用,netty设计数据读取功能包括三个要素:Channel, Eve ...

  2. HTTP协议学习(一)

    一.HTTP报文的组成 请求报文由 请求行.请求头.请求空行.请求实体四部分组成.其中,请求行和请求头共同组成 请求报文头部 请求行:一行,依次由 请求方法.URI(或者应该说是域名?).HTTP协议 ...

  3. centos7安装Wkhtmltopdf

    从官网下载预编译版安装: wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.4/wkhtmltox-0.12 ...

  4. 第8章 CentOS包管理详解

    8.1 Linux上构建C程序的过程 在说明包相关的内容之前,我觉得有必要说一下在Linux上构建一个C程序的过程.我个人并没有学习过C,内容总结自网上,所以可能显得很小白,而且也并非一定正确,只希望 ...

  5. mac os下vscode快捷键

    全局 Command + Shift + P / F1 显示命令面板 Command + P 快速打开 Command + Shift + N 打开新窗口 Command + W 关闭窗口 基本 Co ...

  6. awk 详解

    AWK 简介 AWK是一种优良的文本处理工具.它不仅是 Linux 中也是任何环境中现有的功能最强大的数据处理引擎之一.这种编程及数据操作语言(其名称得自于它的创始人 Alfred Aho .Pete ...

  7. JavaScript定时器实现的原理分析

    原文链接:http://www.cnblogs.com/st-leslie/p/6082450.html 一.储备知识 在我们在项目中一般会遇见过这样的两种定时器,第一种是setTimeOut,第二种 ...

  8. 前端常用技术概述--Less、typescript与webpack

    前言:讲起前端,我们就不能不讲CSS与Javascript,在这两种技术广泛应用的今天,他们的扩展也是层出不穷,css的扩展有Less.Sass.Stylus 等,js的超集有Typescript等. ...

  9. Grafan+Prometheus 监控 MySQL

    架构图 环境 IP 环境 需装软件 192.168.0.237 mysql-5.7.20 node_exporter-0.15.2.linux-amd64.tar.gz mysqld_exporter ...

  10. CSS3布局之多列布局columns详解

    columns语法:columns:[ column-width ] || [ column-count ]设置或检索对象的列数和每列的宽度 其中:[ column-width ]:设置或检索对象每列 ...