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

    1. <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类

    1. 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文件中添加映射信息

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

    1. 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. 【leet-code】135. 加油站

    题目描述 在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升. 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升.你从其 ...

  2. lua的table元类

    Lua中提供的元表是用于帮助Lua数据变量完成某些非预定义功能的个性化行为,如两个table的相加.假设a和b都是table,通过元表可以定义如何计算表达式a+b.当Lua试图将两个table相加时, ...

  3. js如何获取url参数

    匹配URL参数的正则是: var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", ...

  4. [转]Docker修改默认时区

    本文转自:https://www.jianshu.com/p/004ddf941aac 前言 前段时间把公司部分项目迁移到了docker 容器里.查看ngixn反向代理的log时发现时间与正确时间相差 ...

  5. petapoco 实体中字段去掉关联(类似于EF中的NotMap)

    怎么才能让不是数据库表中的字段放在实体中而不影响正常的插入和更新呢? 找到 PetaPoco.cs 文件,打开之后,搜索插入方法(Insert),然后继续找到下一层方法 就能看到如下代码: 看到这个注 ...

  6. C# 读写App.config配置文件

    一.C#项目中添加App.config配置文件 在控制台程序中,默认会有一个App.config配置文件,如果不小心删除掉,或者其他程序需要配置文件,可以通过添加得到. 添加步骤:右键项目名称,选择“ ...

  7. 自定义简单版本python线程池

    python未提供线程池模块,在python3上用threading和queue模块自定义简单线程池,代码如下: #用threading queue 做线程池 import queue import ...

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

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

  9. 淘宝cnpm

    npm install -g cnpm --registry=https://registry.npm.taobao.org

  10. [HTML/CSS]三角形

    CSS盒子模型 当我们把padding和width,height全部设置为0,border设为一个较大的像素时 即:我们需要什么方向的三角形,只需把其余的三角形背景色设置为transparent: