zoukankan      html  css  js  c++  java
  • Hibernate 入门指南

    同时使用面向对象的软件和关系数据库可能既麻烦又耗时。由于对象和关系数据库中数据的表示方式之间存在许多“范式不匹配”​​,因此开发成本显着提高。Hibernate 是 Java 环境的对象/关系映射 (ORM) 解决方案。

    本篇讲解xml和注解两种方式,项目结构如下:

     父项目的pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!--
      ~ Hibernate, Relational Persistence for Idiomatic Java
      ~
      ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
      ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
      -->
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>org.hibernate.tutorials</groupId>
        <artifactId>hibernate-tutorials</artifactId>
        <version>5.4.32.Final</version>
        <packaging>pom</packaging>
    
        <name>Hibernate Getting Started Guide Tutorials</name>
        <description>Aggregator for the Hibernate tutorials presented in the Getting Started Guide</description>
    
        <properties>
            <!-- Skip artifact deployment -->
            <maven.deploy.skip>true</maven.deploy.skip>
        </properties>
    
        <modules>
            <module>basic</module>
            <module>annotations</module>
            <module>entitymanager</module>
            <module>envers</module>
            <module>osgi</module>
        </modules>
    
        <dependencies>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>5.4.32.Final</version>
            </dependency>
    
            <!-- Hibernate uses jboss-logging for logging, for the tutorials we will use the sl4fj-simple backend -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-simple</artifactId>
                <version>1.7.5</version>
            </dependency>
    
            <!-- The tutorials use JUnit test cases to illustrate usage -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
    
            <!-- The tutorials use the H2 in-memory database -->
            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
                <version>1.4.197</version>
            </dependency>
        </dependencies>
    
        <build>
            <testResources>
                <testResource>
                    <filtering>false</filtering>
                    <directory>src/test/java</directory>
                    <includes>
                        <include>**/*.xml</include>
                    </includes>
                </testResource>
                <testResource>
                    <directory>src/test/resources</directory>
                </testResource>
            </testResources>
        </build>
    
    </project>

    1.XML方式(basic项目)

    pom.xml:

    <?xml version="1.0"?>
    <!--
      ~ Hibernate, Relational Persistence for Idiomatic Java
      ~
      ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
      ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
      -->
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
        <modelVersion>4.0.0</modelVersion>
    
        <parent>
            <groupId>org.hibernate.tutorials</groupId>
            <artifactId>hibernate-tutorials</artifactId>
            <version>5.4.32.Final</version>
            <relativePath>../pom.xml</relativePath>
        </parent>
    
        <artifactId>hibernate-tutorial-hbm</artifactId>
        <name>Hibernate hbm.xml Tutorial</name>
        <description>Hibernate tutorial illustrating the use of native APIs and hbm.xml for mapping metadata</description>
    
        <properties>
            <!-- Skip artifact deployment -->
            <maven.deploy.skip>true</maven.deploy.skip>
        </properties>
    
    </project>

    创建实体对象:Event

    package org.hibernate.tutorial.hbm;
    
    import java.util.Date;
    
    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;
        }
    
        public Long getId() {
            return id;
        }
    
        private void setId(Long id) {
            this.id = id;
        }
    
        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;
        }
    }

    创建实体对象对应的XML:Event.hbm.xml

    <?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">
    
    <hibernate-mapping package="org.hibernate.tutorial.hbm">
    
        <class name="Event" table="EVENTS">
            <id name="id" column="EVENT_ID">
                <generator class="increment"/>
            </id>
            <property name="date" type="timestamp" column="EVENT_DATE"/>
            <property name="title"/>
        </class>
    
    </hibernate-mapping>

    创建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>
    
            <!-- Database connection settings -->
            <property name="connection.driver_class">org.h2.Driver</property>
            <property name="connection.url">jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE</property>
            <property name="connection.username">sa</property>
            <property name="connection.password"/>
    
            <!-- 连接池大小 -->
            <property name="connection.pool_size">1</property>
    
            <!-- 方言 -->
            <property name="dialect">org.hibernate.dialect.H2Dialect</property>
    
            <!-- 缓存  -->
            <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
    
            <!-- 显示sql语句 -->
            <property name="show_sql">true</property>
    
            <property name="hbm2ddl.auto">create</property>
    
            <mapping resource="org/hibernate/tutorial/hbm/Event.hbm.xml"/>
    
        </session-factory>
    
    </hibernate-configuration>

    创建测试类:NativeApiIllustrationTest

    package org.hibernate.tutorial.hbm;
    
    import java.util.Date;
    import java.util.List;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.boot.MetadataSources;
    import org.hibernate.boot.registry.StandardServiceRegistry;
    import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
    
    import junit.framework.TestCase;
    
    public class NativeApiIllustrationTest extends TestCase {
        private SessionFactory sessionFactory;
    
        @Override
        protected void setUp() throws Exception {
            // 一个应用公用一个SessionFactory
            final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
                    .configure() // 读取hibernate.cfg.xml
                    .build();
            try {
                sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
            }
            catch (Exception e) {
                StandardServiceRegistryBuilder.destroy( registry );
            }
        }
    
        @Override
        protected void tearDown() throws Exception {
            if ( sessionFactory != null ) {
                sessionFactory.close();
            }
        }
    
        @SuppressWarnings("unchecked")
        public void testBasicUsage() {
            // create a couple of events...
            Session session = sessionFactory.openSession();
            session.beginTransaction();
            session.save( new Event( "第一个event!", new Date() ) );
            session.save( new Event( "第二个event", new Date() ) );
            session.getTransaction().commit();
            session.close();
    
            session = sessionFactory.openSession();
            session.beginTransaction();
            List result = session.createQuery( "from Event" ).list();
            for ( Event event : (List<Event>) result ) {
                System.out.println( "Event (" + event.getDate() + ") : " + event.getTitle() );
            }
            session.getTransaction().commit();
            session.close();
        }
    }

    运行testBasicUsage方法:

    2.使用注解方式(annotations项目)

    pom.xml:

    <?xml version="1.0"?>
    <!--
      ~ Hibernate, Relational Persistence for Idiomatic Java
      ~
      ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
      ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
      -->
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
        <modelVersion>4.0.0</modelVersion>
    
        <parent>
            <groupId>org.hibernate.tutorials</groupId>
            <artifactId>hibernate-tutorials</artifactId>
            <version>5.4.32.Final</version>
            <relativePath>../pom.xml</relativePath>
        </parent>
    
        <artifactId>hibernate-tutorial-annotations</artifactId>
        <name>Hibernate Annotations Tutorial</name>
        <description>Hibernate tutorial illustrating the use of native APIs and annotations for mapping metadata</description>
    
        <properties>
            <!-- Skip artifact deployment -->
            <maven.deploy.skip>true</maven.deploy.skip>
        </properties>
    
    </project>

    创建实体类:Event

    package org.hibernate.tutorial.annotations;
    
    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;
        }
    }

    创建实体类对应的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>
    
            <!-- Database connection settings -->
            <property name="connection.driver_class">org.h2.Driver</property>
            <property name="connection.url">jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE</property>
            <property name="connection.username">sa</property>
            <property name="connection.password"></property>
    
            <!-- JDBC connection pool (use the built-in) -->
            <property name="connection.pool_size">1</property>
    
            <!-- SQL dialect -->
            <property name="dialect">org.hibernate.dialect.H2Dialect</property>
    
            <!-- Disable the second-level cache  -->
            <property name="cache.provider_class">org.hibernate.cache.internal.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">create</property>
    
            <!-- Names the annotated entity class -->
            <mapping class="org.hibernate.tutorial.annotations.Event"/>
    
        </session-factory>
    
    </hibernate-configuration>

    创建测试类:AnnotationsIllustrationTest

    package org.hibernate.tutorial.annotations;
    
    import java.util.Date;
    import java.util.List;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.boot.MetadataSources;
    import org.hibernate.boot.registry.StandardServiceRegistry;
    import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
    
    import junit.framework.TestCase;
    
    public class AnnotationsIllustrationTest extends TestCase {
        private SessionFactory sessionFactory;
    
        @Override
        protected void setUp() throws Exception {
            // A SessionFactory is set up once for an application!
            final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
                    .configure() // configures settings from hibernate.cfg.xml
                    .build();
            try {
                sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
            }
            catch (Exception e) {
                // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
                // so destroy it manually.
                StandardServiceRegistryBuilder.destroy( registry );
            }
        }
    
        @Override
        protected void tearDown() throws Exception {
            if ( sessionFactory != null ) {
                sessionFactory.close();
            }
        }
    
        @SuppressWarnings({ "unchecked" })
        public void testBasicUsage() {
            // create a couple of events...
            Session session = sessionFactory.openSession();
            session.beginTransaction();
            session.save( new Event( "测试1", new Date() ) );
            session.save( new Event( "测试2", new Date() ) );
            session.getTransaction().commit();
            session.close();
    
            // now lets pull events from the database and list them
            session = sessionFactory.openSession();
            session.beginTransaction();
            List result = session.createQuery( "from Event" ).list();
            for ( Event event : (List<Event>) result ) {
                System.out.println( "Event (" + event.getDate() + ") : " + event.getTitle() );
            }
            session.getTransaction().commit();
            session.close();
        }
    }

    运行testBasicUsage方法:

  • 相关阅读:
    ES6标准入门之正则表达式的拓展
    将博客搬至CSDN
    CUDA杂谈
    QT源码解析笔记
    图解DTS和PTS
    图解 I帧,B帧以及P帧
    这半年的一些事情
    C++的一些编程规范
    pjsip与QT进行适配
    编程规范检测脚本
  • 原文地址:https://www.cnblogs.com/wuwuyong/p/15243148.html
Copyright © 2011-2022 走看看