JPA 2.1实例(hibernate 实现)

1.环境准备

1)java se 7

2)maven 3

3)mysql database

2.创建数据库和表结构

首先创建数据库。创建数据库脚本如下:

create database jpa;

创建表语句如下:

CREATE TABLE `products` (                                
            `id` int(11) NOT NULL AUTO_INCREMENT,                  
            `product_name` varchar(250) DEFAULT NULL,              
            `product_description` varchar(400) DEFAULT NULL,       
            `stock_qty` double(10,2) DEFAULT NULL,                 
            `price` double(10,2) DEFAULT NULL,                     
            PRIMARY KEY (`id`)                                     
   ) ENGINE=InnoDB;

3.创建maven项目

使用myeclipse 创建maven项目,创建完成后目录结构如下图:

image

 

4.设置项目依赖POM.XML文件

   代码如下所示:

<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>com.johnfung.study</groupId>
  <artifactId>jpa</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>jpa</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.28</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>  
  </dependencies>
</project>

5.创建Entity Class

下面的class为product.java

package com.johnfung.study.jpa;

import javax.persistence.*;

/**
* @author JohnFung
* More info at http://www.cnblogs.com/johnfung/
*/

@Entity
@Table(name="products")
public class Product {
        
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private int id;
        
        @Column(name="product_name")
        private String productName;
        
        @Column(name="product_description")
        private String productDescription;
        
        @Column(name="stock_qty")
        private Double stockQty;
        
        @Column(name="price")
        private Double price;
         
        public Product(){};
        public Product(String productName,String productDescription,Double price){
            this.productName=productName;
            this.productDescription=productDescription;
            this.price=price;
        }
        
        public int getId() {
                return id;
        }

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

        public String getProductName() {
                return productName;
        }

        public void setProductName(String productName) {
                this.productName = productName;
        }

        public String getProductDescription() {
                return productDescription;
        }

        public void setProductDescription(String productDescription) {
                this.productDescription = productDescription;
        }

        public Double getStockQty() {
                return stockQty;
        }

        public void setStockQty(Double stockQty) {
                this.stockQty = stockQty;
        }

        public Double getPrice() {
                return price;
        }

        public void setPrice(Double price) {
                this.price = price;
        }
        

}

6.编写测试代码

代码编写如下:

package com.johnfung.study.jpa;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import org.junit.Test;

/**
 * Unit test for simple App.
 */
public class AppTest 
{
    @Test
    public void addProduct(){
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("persistenceUnit");
        EntityManager manager = factory.createEntityManager();
        EntityTransaction tx = manager.getTransaction();
        tx.begin();
        try {
        int numOfEmployees = manager.createQuery("Select a From Product a", Product.class).getResultList().size();
        if (numOfEmployees == 0) {
            Product product = new Product("book","jpa book",34.4);
            manager.persist(product);            
        }
        } catch (Exception e) {
            e.printStackTrace();
        }
        tx.commit();
    }
}
到此所有工作都已完成,可以测试程序是否运行成功。
posted @ 2014-02-11 15:27  JohnFung  阅读(2763)  评论(0编辑  收藏  举报