当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
计算机二级辅导:JPA学习笔记(一)
发布时间:2010/4/5 17:15:36 来源:城市学习网 编辑:admin

  JPA学习笔记(一)
  一般来讲JPA包含以下几个文件:
  1.pojo类
  2.映射文件
  3.持久层映射文件(Persistence units)
  1,2主要用来映射关系数据库中的实体,3主要用来定义JPAProvide,数据库连接定义等操作.
  下面来看具体的POJO类的写法
  packagecom.liliang.entity;
  importjavax.persistence.Column;

  importjavax.persistence.Entity;
  importjavax.persistence.GeneratedValue;
  importjavax.persistence.GenerationType;
  importjavax.persistence.Id;
  importjavax.persistence.Version;
  importjava.io.Serializable;
  importjava.util.Date;
  //JPA必须拥有一个实体类,对应数据库中的表.
  //实体类的要求是:
  //1.首先需要用注解的方式在类开头声明:@Entity()声明
  //2.必须包含一个空的构造函数,并且必须是public或是protected修饰符.
  //3.必须是top-levle类,即enum或是interface是不可以用作当成实体类的.
  //4.不可以是final.如果实体类有可能是托管对象,那么就必须实现Serialization接口.
  //我们使用注解的方式来实现.
  //关于注解的解释:
  //1.@Columnprovidesthenameofthecolumninatableifitisdifferentfromtheattributename.
  //(Bydefault,thetwonamesareassumedtobethesame.)
  //
  //2.JPAallowspersistentclassestoinheritfromnon-persistentclasses,persistentclassestoinheritfromotherpersistentclasses,
  //andnon-persistentclassestoinheritfrompersistentclasses.
  //3.Theentityclassshouldhaveadefaultno-argumentconstructor.
  //4.Theentityclassshouldnotbefinal.
  //5.Persistentclassescannotinheritfromcertainnatively-implementedsystemclassessuchasjava.net.Socketandjava.lang.Thread.
  //6.Ifapersistentclassinheritsfromanon-persistentclass,thefieldsofthenon-persistentsuperclasscannotbepersisted.
  @Entity(name="Customer")
  publicclassCustomerimplementsSerializable{
  /**
  *主机自动生成
  */
  privatestaticfinallongserialVersionUID=-4020535715918096623L;
  @Id//代表主键
  @Column(name="CUST_ID",nullable=false)//映射表中的一列
  @GeneratedValue(strategy=GenerationType.AUTO)//标示符生成策略
  privateIntegercustId;
  @Column(name="FIRST_NAME",nullable=true,length=50)
  privateStringfirst_Name;
  @Column(name="LAST_NAME",nullable=false,length=50)
  privateStringlast_Name;
  @Column(name="STREET",nullable=false,length=50)
  privateStringstreet;
  @Column(name="APPT",nullable=true,length=20)
  privateStringappt;
  @Column(name="CITY",nullable=false,length=25)
  privateStringcity;
  @Column(name="ZIP_CODE",nullable=true,length=10)
  privateStringzipCode;
  @Column(name="CUST_TYPE",nullable=true,length=10)
  privateStringcustType;
  @Version
  @Column(name="LAST_UPDATE_TIME",nullable=true)
  privateDateupdateTime;
  //默认
  Customer()Customer(){};
  //最小填充
  Customer()Customer(IntegercustId,Stringlast_Name,Stringstreet,Stringcity){
  this.custId=custId;
  this.last_Name=last_Name;
  this.street=street;
  this.city=city;
  }
  //getterandsettermethod
  好了,下面来看映射文件的写法
  ?xmlversion="1.0"encoding="UTF-8"?
  !
  1.该xml文件可以包含多个persistent元素,每个元素都可以定义自己的持久层实现类和数据库2.实体类在class标签中声明
  persistencexmlns="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"
  persistence-unitname="testjpa"transaction-type="RESOURCE_LOCAL"
  !使用OpenJPA持久层实现
  !
  ProviderclassthatsuppliesEntityManagersforthispersistenceunit
  provider
  org.apache.openjpa.persistence.PersistenceProviderImpl
  /provider
  !映射实体类
  classcom.liliang.entity.Customer/class
  !Alistofvendor-specificproperties.
  properties
  propertyname="openjpa.ConnectionURL"value="jdbc:mysql://localhost:3306/hibernate"/
  propertyname="openjpa.ConnectionDriverName"value="com.mysql.jdbc.Driver"/
  propertyname="openjpa.ConnectionUserName"value="root"/
  propertyname="openjpa.ConnectionPassword"value="1d280478"/
  propertyname="openjpa.Log"value="SQL=TRANCE"/
  /properties
  /persistence-unit
  /persistence
  :

广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved