JPA Hibernate应用实例

1.配置文件:persistence.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
 3     <persistence-unit name="TestJPA" transaction-type="RESOURCE_LOCAL">
 4     
 5         <!-- JAP实现的提供者
 6         1.如果只有一个提供者,可以不写 -->
 7         <provider>org.hibernate.ejb.HibernatePersistence</provider>
 8         
 9         <class>com.hanqi.dao.JPANews</class>
10     
11         <properties>
12         
13             <!-- 数据库连接 -->
14             <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
15             <property name="javax.persistence.jdbc.user" value="test"/>
16             <property name="javax.persistence.jdbc.password" value="test"/>
17             <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
18             
19             <!-- JPA提供者的配置 -->
20             <!-- 数据库方言 -->
21             <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
22             <!-- sql语句/调试 -->
23             <property name="hibernate.show_sql" value="true" />
24             <property name="hibernate.format_sql" value="true" />
25              <!--  自动建表(正向工程)方式 -->
26             <property name="hibernate.hbm2ddl.auto" value="update" />
27             
28         </properties>
29     
30     </persistence-unit>
31 </persistence>

2.编写实体类:JPANews.java

 1 package com.hanqi.dao;
 2 
 3 import java.util.Date;
 4 
 5 import javax.persistence.*;
 6 @NamedQuery(name="cx",query="from JPANews n where n.id = ?")
 7 @Table(name="JPA_News")//类与表之间的映射关系
 8 @Entity //实体类
 9 public class JPANews {
10     
11     private Integer id;
12     private String title;
13     private String contant;
14     private Date createdate;
15     private String author;
16     
17     @GeneratedValue(strategy=GenerationType.AUTO)//主键生成策略
18     @Id//指定主键
19     public Integer getId() {
20         return id;
21     }
22     public void setId(Integer id) {
23         this.id = id;
24     }
25     
26     @Basic
27     public String getTitle() {
28         return title;
29     }
30     public void setTitle(String title) {
31         this.title = title;
32     }
33     public String getContant() {
34         return contant;
35     }
36     public void setContant(String contant) {
37         this.contant = contant;
38     }
39     
40     /**
41      * @return the createdate
42      */
43     @Column(name="CREATEDATE")
44     @Basic 
45     public Date getCreatedate() {
46         return createdate;
47     }
48     /**
49      * @param createdate the createdate to set
50      */
51     public void setCreatedate(Date createdate) {
52         this.createdate = createdate;
53     }
54     /**
55      * @return the author
56      */
57     public String getAuthor() {
58         return author;
59     }
60     /**
61      * @param author the author to set
62      */
63     public void setAuthor(String author) {
64         this.author = author;
65     }
66     
67     @Override
68     @Transient
69     public String toString() {
70         return "News [id=" + id + ", title=" + title + ", contant=" + contant + ", createdate=" + createdate + "]";
71     }
72 
73 }

3.测试用例TestJPA.java

  1 package com.hanqi.dao;
  2 
  3 import static org.junit.Assert.*;
  4 
  5 import java.util.Date;
  6 import java.util.List;
  7 
  8 import javax.persistence.EntityManager;
  9 import javax.persistence.EntityManagerFactory;
 10 import javax.persistence.EntityTransaction;
 11 import javax.persistence.Persistence;
 12 import javax.persistence.Query;
 13 
 14 import org.junit.*;
 15 import org.junit.Test;
 16 
 17 public class TestJPA {
 18     EntityManagerFactory entityManagerFactory = null;
 19     EntityManager entityManager = null;
 20     EntityTransaction transaction = null;
 21     
 22     @Before
 23     public void init()
 24     {
 25         //1. 创建 EntitymanagerFactory
 26         String persistenceUnitName = "TestJPA";
 27 
 28         entityManagerFactory = 
 29                 Persistence.createEntityManagerFactory(persistenceUnitName);
 30         
 31 
 32         //2. 创建 EntityManager. 类似于 Hibernate 的 SessionFactory
 33         entityManager = entityManagerFactory.createEntityManager();
 34         
 35         //3. 开启事务
 36         transaction = entityManager.getTransaction();
 37         transaction.begin();
 38     }
 39     
 40     @After
 41     public void destory()
 42     {
 43         //5. 提交事务
 44         transaction.commit();
 45         
 46         //6. 关闭 EntityManager
 47         entityManager.close();
 48         
 49         //7. 关闭 EntityManagerFactory
 50         entityManagerFactory.close();
 51     }
 52 
 53     @Test
 54     public void testjpa()
 55     {
 56         
 57         //数据操作
 58         JPANews jn = new JPANews();
 59         
 60         jn.setTitle("标题");
 61         jn.setAuthor("作者");
 62         jn.setContant("内容");
 63         jn.setCreatedate(new Date());
 64 //        
 65 //        entityManager.persist(jn);//保存
 66         JPANews jn2 = entityManager.merge(jn);//saveOrUpdate
 67         
 68         
 69         
 70         System.out.println(jn);
 71         System.out.println(jn2);//从瞬时状态转变为持久化状态jpa是创建一个新对象将旧对象信息拷贝到新对象中
 72 
 73 //        JPANews jpan = entityManager.find(JPANews.class, 41);//立即加载
 74 //        
 75 //        jpan.setTitle("新的标题123");
 76 //        
 77 //        entityManager.flush();//提交语句
 78 //        System.out.println(jpan);
 79 //        //延迟加载
 80 //        JPANews jpan2 = entityManager.getReference(JPANews.class, 42);
 81 //        
 82 //        System.out.println("id="+jpan2.getId());
 83 //        System.out.println("标题="+jpan2.getTitle());
 84 //        
 85 //        entityManager.remove(jpan2);//删除
 86         
 87     }
 88     
 89     @Test
 90     public void testjpql()
 91     {
 92         //JPQL语句
 93         String jpql = "from JPANews n where n.id = ?";
 94         
 95         Query q = entityManager.createQuery(jpql);
 96         
 97 //        Query q = entityManager.createNamedQuery("cx");//执行实体类注解中的jpql语句
 98         
 99         q.setParameter(1, 41);//添加jpql语句参数,占位符编号从1开始
100         
101         List<JPANews> l = q.getResultList();
102         
103         System.out.println("长度=" + l.size());
104         
105     }
106 
107 }
原文地址:https://www.cnblogs.com/dirgo/p/5141149.html