配色: 字号:
JPA实体bean配置,jpa增删改api,jpasql增删改
2016-09-01 | 阅:  转:  |  分享 
  
JPA实体bean配置,jpa增删改api,jpasql增删改



1.ORM框架必然发展趋势:

jdbc->hibernate(是产品,实现jpa规范)->jpa(是规范,不是产品)。

ps:运用jpa规范的API进行编程,不对Hiberbate,topLink等orm框架构成威胁。



2.JPA环境搭建[hibernate-distribution-3.6.10.Final]

1.准备lib包

2.jar包引入时,千万注意目录不能有中文或者空格



3.开发步骤:

1.先建表,再编写配置文件和bean-(面向过程,传统的数据库建模思想)

2.先编写配置文件和bean,在建表(OOP思想)-要求比较高



4.demo实例

事务种类:

1.本地事务:支持对同一个数据库的事务操作——大部分应用

2.全局事务:支持对多个数据库的事务操作(银行转账)-两次提交协议

步骤:

第一步:项目结构



2.持久化文件配置:

[html]viewplaincopyprint?




xmlns:xsi="http://driverw3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://localhost:3306/xml/ns/persistencehttp://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"

version="2.0">

























3.实体bean

知识点:字段的长度,是否为空,关键字,自增,字段名称的映射修改,表名称的映射修改,字段类型(Date类型)-不同格式要求,枚举类的注释(索引,枚举值)-性别,大文本类型数据,二进制数据映射,不想某个字段跟表有映射关系,为了防止某个字段数据量过大而占用内存过大因此对其进行延迟加载(懒惰加载,需要获取数据时才得到数据)。

[java]viewplaincopyprint?

importjava.util.Date;

importjavax.persistence.Basic;

importjavax.persistence.Column;

importjavax.persistence.Entity;

importjavax.persistence.EnumType;

importjavax.persistence.Enumerated;

importjavax.persistence.FetchType;

importjavax.persistence.GeneratedValue;

importjavax.persistence.Id;

importjavax.persistence.Lob;

importjavax.persistence.Table;

importjavax.persistence.Temporal;

importjavax.persistence.TemporalType;

importjavax.persistence.Transient;



@Entity

@Table(name="person")

publicclassPerson{

privateIntegerid;

privateStringname;

privateDatebirthday;

privateSexsex;

privateStringinfo;

privateByte[]file;

privateStringother;





publicPerson(){

super();

}

publicPerson(Stringname){

super();

this.name=name;

}

publicPerson(Stringname,Datebirthday){

super();

this.name=name;

this.birthday=birthday;

}



publicPerson(Stringname,Datebirthday,Sexsex){

super();

this.name=name;

this.birthday=birthday;

this.sex=sex;

}

/

主键并自增

@returntheid

/

@Id@GeneratedValue

publicIntegergetId(){

returnid;

}

/

@paramidtheidtoset

/

publicvoidsetId(Integerid){

this.id=id;

}

/

@returnthename

/

@Column(length=10,nullable=false,name="personName")

publicStringgetName(){

returnname;

}

/

@paramnamethenametoset

/

publicvoidsetName(Stringname){

this.name=name;

}

/

@returnthebirthday

/

@Temporal(TemporalType.DATE)

publicDategetBirthday(){

returnbirthday;

}

/

@parambirthdaythebirthdaytoset

/

publicvoidsetBirthday(Datebirthday){

this.birthday=birthday;

}

/

@returnthesex

/

@Enumerated(EnumType.STRING)

publicSexgetSex(){

returnsex;

}

/

@paramsexthesextoset

/

publicvoidsetSex(Sexsex){

this.sex=sex;

}

/

@returntheinfo

/

@Lob

publicStringgetInfo(){

returninfo;

}

/

@paraminfotheinfotoset

/

publicvoidsetInfo(Stringinfo){

this.info=info;

}

/

@returnthefile

/

@Lob@Basic(fetch=FetchType.LAZY)//当文件很大时,进行懒惰加载

publicByte[]getFile(){

returnfile;

}

/

@paramfilethefiletoset

/

publicvoidsetFile(Byte[]file){

this.file=file;

}

/

@returntheother

/

@Transient//排除某个字段的映射

publicStringgetOther(){

returnother;

}

/

@paramothertheothertoset

/

publicvoidsetOther(Stringother){

this.other=other;

}



}

枚举类:

[java]viewplaincopyprint?

publicenumSex{

MAN,WORMAN

}



4.单元测试类

知识点:

1.把握异常出现的时机。

2.通过ID得到实体bean(1.彻底查询2.用到查询)

3.保存实体bean到数据库

4.更新实体bean到数据库中

涉及到对象的状态:

1.新建

2.托管(设置实体的字段值,并通过提交可以同步到数据库)

3.游离(无法更新到数据库中,除非使用merge方法重新可将其更新到数据库中)

4.删除

[java]viewplaincopyprint?

publicclassPersonTest{

@Test

publicvoidsave(){

EntityManagerFactoryfactory=Persistence.createEntityManagerFactory("MyJpa");

EntityManagerem=factory.createEntityManager();

em.getTransaction().begin();

em.persist(newPerson("techbirds",newDate(),Sex.MAN));

em.getTransaction().commit();

em.close();

factory.close();



}

@Test

publicvoidgetPerson1(){

EntityManagerFactoryfactory=Persistence.createEntityManagerFactory("MyJpa");

EntityManagerem=factory.createEntityManager();

em.getTransaction().begin();

Personp=em.find(Person.class,1);

em.getTransaction().commit();

em.close();

factory.close();

System.out.println(p.getName());

}

@Test

publicvoidgetPerson2(){

EntityManagerFactoryfactory=Persistence.createEntityManagerFactory("MyJpa");

EntityManagerem=factory.createEntityManager();

em.getTransaction().begin();

Personp=em.getReference(Person.class,1);

//代理对象,用到才查询

System.out.println(p.getName());

em.getTransaction().commit();

em.close();

//System.out.println(p.getName());出错,事务已经关闭

factory.close();



}

@Test

publicvoidupdatePerson1(){

EntityManagerFactoryfactory=Persistence.createEntityManagerFactory("MyJpa");

EntityManagerem=factory.createEntityManager();

em.getTransaction().begin();

Personp=em.find(Person.class,1);

p.setName("bao");

em.getTransaction().commit();

em.close();

factory.close();

}

@Test

publicvoidupdatePerson2(){

EntityManagerFactoryfactory=Persistence.createEntityManagerFactory("MyJpa");

EntityManagerem=factory.createEntityManager();

em.getTransaction().begin();

Personp=em.find(Person.class,1);

em.clear();//将所有实体管理器中的所有实体变成游离状态,无法跟数据库同步

p.setName("techbirds");

em.getTransaction().commit();

em.close();

factory.close();

publicvoidupdatePerson3(){

EntityManagerFactoryfactory=Persistence.createEntityManagerFactory("MyJpa");

EntityManagerem=factory.createEntityManager();

em.getTranwww.wang027.comsaction().begin();

Personp=em.find(Person.class,1);

em.clear();//将所有实体管理器中的所有实体变成游离状态,无法跟数据库同步

p.setName("techbirds");

em.merge(p);//此时又可以进行同步

em.getTransaction().commit();

em.close();

factory.close();

}



@Test

publicvoiddelPerson(){

EntityManagerFactoryfactory=Persistence.createEntityManagerFactory("MyJpa");

EntityManagerem=factory.createEntityManager();

em.getTransaction().begin();

Personp=em.find(Person.class,1);

em.remove(p);

em.getTransaction().commit();

em.close();

factory.close();

}



}



5.jpa的(sql)查询

jpaSQL语句:面向对象的sql语句,jpa标准的sql语法

查询方法:

1.位参数查询selectofromPersonowhereo.id=?1—>query.setParameter(1,2);

2.命名查询selectofromPersonowhereo.id=:id—>query.setParameter("id",2);

查询结果:1.列表2.唯一值(对象)

查询类型:普通查询,删除查询,更新查询

ps:进行数据的更改必须启动事务。-删除查询和更新查询必须开启事务

[java]viewplaincopyprint?

@Test

publicvoidquerysql(){

EntityManagerFactoryfactory=Persistence.createEntityManagerFactory("MyJpa");

EntityManagerem=factory.createEntityManager();

//面向对象的sql语句

Queryquery=em.createQuery("selectofromPersonowhereo.id=?");

query.setParameter(1,2);

Personp=(Person)query.getSingleResult();

System.out.println(p.getName());

em.close();

factory.close();



}



@Test

publicvoiddeletesql(){

EntityManagerFactoryfactory=Persistence.createEntityManagerFactory("MyJpa");

EntityManagerem=factory.createEntityManager();

em.getTransaction().begin();

//面向对象的sql语句

Queryquery=em.createQuery("deletefromPersonowhereo.id=?");

query.setParameter(1,3);

query.executeUpdate();

em.getTransaction().commit();

em.close();

factory.close();



}



@Test

publicvoidupdatesql(){

EntityManagerFactoryfactory=Persistence.createEntityManagerFactory("MyJpa");

EntityManagerem=factory.createEntityManager();

em.getTransaction().begin();

//面向对象的sql语句

Queryquery=em.createQuery("updatePersonoseto.sex=?whereo.id=?");

query.setParameter(1,Sex.WORMAN);

query.setParameter(2,3);

query.executeUpdate();

em.getTransaction().commit();

em.close();

factory.close();



}

献花(0)
+1
(本文系thedust79首藏)