Hibernate 5 入门指南-基于JPA

  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. 运行测试

上一篇:hihocoder-1347 小h的树上的朋友(lca+线段树)


下一篇:HTML与CSS简单页面效果实例