JPA+Hibernate 3.3 学习小结——第一个JPA程序

来源:互联网 发布:linux find 查找文件名 编辑:程序博客网 时间:2024/06/07 05:41

 所需要的最小的jar(注意:jar包所在路径不能含有空格或中文)


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->hibernate3.jar
hibernate
-cglib-repack-2.1_3.jar
slf4j
-api-1.5.2.jar
javassist
-3.4.GA.jar
jta
-1.1.jar
antlr
-2.7.6.jar
commons
-collections-3.1.jar
dom4j
-1.6.1.jar
ejb3
-persistence.jar
hibernate
-annotations.jar
hibernate
-commons-annotations.jar
hibernate
-entitymanager.jar
log4j.jar
slf4j
-log4j12.jar

<!-- /* Font Definitions */&#64;font-face{font-family:宋体;panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-alt:SimSun;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 680460288 22 0 262145 0;}&#64;font-face{font-family:"Cambria Math";panose-1:2 4 5 3 5 4 6 3 2 4;mso-font-charset:1;mso-generic-font-family:roman;mso-font-format:other;mso-font-pitch:variable;mso-font-signature:0 0 0 0 0 0;}&#64;font-face{font-family:Calibri;panose-1:2 15 5 2 2 2 4 3 2 4;mso-font-charset:0;mso-generic-font-family:swiss;mso-font-pitch:variable;mso-font-signature:-1610611985 1073750139 0 0 159 0;}&#64;font-face{font-family:微软雅黑;panose-1:2 11 5 3 2 2 4 2 2 4;mso-font-charset:134;mso-generic-font-family:swiss;mso-font-pitch:variable;mso-font-signature:-2147483001 705641554 22 0 262175 0;}&#64;font-face{font-family:"/&#64;微软雅黑";panose-1:2 11 5 3 2 2 4 2 2 4;mso-font-charset:134;mso-generic-font-family:swiss;mso-font-pitch:variable;mso-font-signature:-2147483001 705641554 22 0 262175 0;}&#64;font-face{font-family:"/&#64;宋体";panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 680460288 22 0 262145 0;}/* Style Definitions */p.MsoNormal, li.MsoNormal, div.MsoNormal{mso-style-unhide:no;mso-style-qformat:yes;mso-style-parent:"";margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;mso-pagination:none;font-size:10.5pt;mso-bidi-font-size:11.0pt;font-family:"Calibri","sans-serif";mso-ascii-font-family:Calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"Times New Roman";mso-bidi-theme-font:minor-bidi;mso-font-kerning:1.0pt;}p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph{mso-style-priority:34;mso-style-unhide:no;mso-style-qformat:yes;margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;text-indent:21.0pt;mso-char-indent-count:2.0;mso-pagination:none;font-size:10.5pt;mso-bidi-font-size:11.0pt;font-family:"Calibri","sans-serif";mso-ascii-font-family:Calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"Times New Roman";mso-bidi-theme-font:minor-bidi;mso-font-kerning:1.0pt;}.MsoChpDefault{mso-style-type:export-only;mso-default-props:yes;mso-bidi-font-family:"Times New Roman";mso-bidi-theme-font:minor-bidi;}/* Page Definitions */&#64;page{mso-page-border-surround-header:no;mso-page-border-surround-footer:no;}&#64;page Section1{size:612.0pt 792.0pt;margin:72.0pt 90.0pt 72.0pt 90.0pt;mso-header-margin:36.0pt;mso-footer-margin:36.0pt;mso-paper-source:0;}div.Section1{page:Section1;}/* List Definitions */&#64;list l0{mso-list-id:1964846596;mso-list-type:hybrid;mso-list-template-ids:536006596 247102048 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;}&#64;list l0:level1{mso-level-tab-stop:none;mso-level-number-position:left;margin-left:18.0pt;text-indent:-18.0pt;}ol{margin-bottom:0cm;}ul{margin-bottom:0cm;}--> 创建实例类


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->package com.hujuan.bean;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
//默认情况下表名称是根据实体类名称创建的,name可以修改表名称
@Table(name="person_table")
public class Person {
    
    
private Integer id;
    
private String userName;
    
    
public Person(){
    }
    
    
public Person(String userName) {
        
this.userName = userName;
    }
    
//主键生成策略@GeneratedValue(strategy=GenerationType.AUTO),
    
//值为AUTO表示根据数据库由Hibernate自动选择生成策略,也可以省略写@GeneratedValue
    
//值为IDENTITY表示主键自增长
    
//值为SEQUENCE表示主键采用序列的方式
    
//值为TABLE各个数据库都通用,但效率较低
    @Id  @GeneratedValue
    
public Integer getId() {
        
return id;
    }
    
public void setId(Integer id) {
        
this.id = id;
    }
    
public String getUserName() {
        
return userName;
    }
    
public void setUserName(String userName) {
        
this.userName = userName;
    }
}
<!-- /* Font Definitions */&#64;font-face{font-family:宋体;panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-alt:SimSun;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 680460288 22 0 262145 0;}&#64;font-face{font-family:"Cambria Math";panose-1:2 4 5 3 5 4 6 3 2 4;mso-font-charset:1;mso-generic-font-family:roman;mso-font-format:other;mso-font-pitch:variable;mso-font-signature:0 0 0 0 0 0;}&#64;font-face{font-family:Calibri;panose-1:2 15 5 2 2 2 4 3 2 4;mso-font-charset:0;mso-generic-font-family:swiss;mso-font-pitch:variable;mso-font-signature:-1610611985 1073750139 0 0 159 0;}&#64;font-face{font-family:微软雅黑;panose-1:2 11 5 3 2 2 4 2 2 4;mso-font-charset:134;mso-generic-font-family:swiss;mso-font-pitch:variable;mso-font-signature:-2147483001 705641554 22 0 262175 0;}&#64;font-face{font-family:"/&#64;微软雅黑";panose-1:2 11 5 3 2 2 4 2 2 4;mso-font-charset:134;mso-generic-font-family:swiss;mso-font-pitch:variable;mso-font-signature:-2147483001 705641554 22 0 262175 0;}&#64;font-face{font-family:"/&#64;宋体";panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 680460288 22 0 262145 0;}/* Style Definitions */p.MsoNormal, li.MsoNormal, div.MsoNormal{mso-style-unhide:no;mso-style-qformat:yes;mso-style-parent:"";margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;mso-pagination:none;font-size:10.5pt;mso-bidi-font-size:11.0pt;font-family:"Calibri","sans-serif";mso-ascii-font-family:Calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"Times New Roman";mso-bidi-theme-font:minor-bidi;mso-font-kerning:1.0pt;}.MsoChpDefault{mso-style-type:export-only;mso-default-props:yes;mso-bidi-font-family:"Times New Roman";mso-bidi-theme-font:minor-bidi;}/* Page Definitions */&#64;page{mso-page-border-surround-header:no;mso-page-border-surround-footer:no;}&#64;page Section1{size:612.0pt 792.0pt;margin:72.0pt 90.0pt 72.0pt 90.0pt;mso-header-margin:36.0pt;mso-footer-margin:36.0pt;mso-paper-source:0;}div.Section1{page:Section1;}-->

注意:因为采用的是Hibernate所以必须有一个空的构造函数

JPA的配置文件

JPA规范要求配置文件在类路径的META-INF目录下放置名称为固定的的,即persistence.xml

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><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_1_0.xsd"
                version
="1.0">
    <!-- 持久化单元,transaction-type事务类型包括全局事务类型JTA和本地事务类型 RESOURCE_LOCAL-->
   
<persistence-unit name="jpa" transaction-type="RESOURCE_LOCAL">
    
<properties>
              
<!-- 数据库方言 -->
 
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>    <!-- 建表方式,value值为creat-drop时表示创建应用的时候建表,结束应用的时候表自动删除;
     值为update表示如果映射元数据不存在则建立表,如果映射元数据存在并新增加了字段则会添加到数据库表中 
-->
        
<property name="hibernate.hbm2ddl.auto" value="update"/>
         
<property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver"/>
         
<property name="hibernate.connection.username" value="root"/>
         
<property name="hibernate.connection.password" value="root"/>
         
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/jpadb"/>      
      
</properties>
   
</persistence-unit>
</persistence>
<!-- /* Font Definitions */&#64;font-face{font-family:宋体;panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-alt:SimSun;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 680460288 22 0 262145 0;}&#64;font-face{font-family:"Cambria Math";panose-1:2 4 5 3 5 4 6 3 2 4;mso-font-charset:1;mso-generic-font-family:roman;mso-font-format:other;mso-font-pitch:variable;mso-font-signature:0 0 0 0 0 0;}&#64;font-face{font-family:Calibri;panose-1:2 15 5 2 2 2 4 3 2 4;mso-font-charset:0;mso-generic-font-family:swiss;mso-font-pitch:variable;mso-font-signature:-1610611985 1073750139 0 0 159 0;}&#64;font-face{font-family:微软雅黑;panose-1:2 11 5 3 2 2 4 2 2 4;mso-font-charset:134;mso-generic-font-family:swiss;mso-font-pitch:variable;mso-font-signature:-2147483001 705641554 22 0 262175 0;}&#64;font-face{font-family:"/&#64;微软雅黑";panose-1:2 11 5 3 2 2 4 2 2 4;mso-font-charset:134;mso-generic-font-family:swiss;mso-font-pitch:variable;mso-font-signature:-2147483001 705641554 22 0 262175 0;}&#64;font-face{font-family:"/&#64;宋体";panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 680460288 22 0 262145 0;}/* Style Definitions */p.MsoNormal, li.MsoNormal, div.MsoNormal{mso-style-unhide:no;mso-style-qformat:yes;mso-style-parent:"";margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;mso-pagination:none;font-size:10.5pt;mso-bidi-font-size:11.0pt;font-family:"Calibri","sans-serif";mso-ascii-font-family:Calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin;mso-bidi-font-family:"Times New Roman";mso-bidi-theme-font:minor-bidi;mso-font-kerning:1.0pt;}.MsoChpDefault{mso-style-type:export-only;mso-default-props:yes;mso-bidi-font-family:"Times New Roman";mso-bidi-theme-font:minor-bidi;}/* Page Definitions */&#64;page{mso-page-border-surround-header:no;mso-page-border-surround-footer:no;}&#64;page Section1{size:612.0pt 792.0pt;margin:72.0pt 90.0pt 72.0pt 90.0pt;mso-header-margin:36.0pt;mso-footer-margin:36.0pt;mso-paper-source:0;}div.Section1{page:Section1;}-->测试类

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->package com.hujuan.test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.junit.BeforeClass;
import org.junit.Test;
import com.hujuan.bean.Person;
public class PersonTest {
    @BeforeClass
    
public static void setUpBeforeClass() throws Exception {
    }
     @Test
    
public void save(){
//EntityManagerFactory相当于Hibernate中的sessionFactory
//Persistence.createEntityManagerFactory("jpa")与配置文件中的持久化单元名称必须相同
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa");
//EntityManager相当于Hibernate中session
        EntityManager em = factory.createEntityManager();
        em.getTransaction().begin();
        
//保存(持久化)方法
        em.persist(new Person("Tom"));
        em.getTransaction().commit();
        em.close();
        factory.close();
    }
}
原创粉丝点击