标签:style blog io color ar os 使用 java sp
,JPA(Java Persistence API):通过注解或XML描述对象--关系表的映射关系,并将运行期的实体对象持久化到数据库中。
JPA是一套规范,不是某个ORM产品,它主要包括以下3方面的技术:
@Entity用来标注实体
@Table用来指定实体对应的表名
@Id用来标注实体的标示,即主键。@GeneratedValue用来标注主键的生成方式。主要有以下几种:
* GenerationType.AUTO---由ORM产品根据数据库方言决定--AUTO是默认值,可以省略,即@Id @GeneratedValue
* GenerationType.IDENTITY---常用于Mysql、Sqlserver
* GenerationType.SEQUENCE---常用于Oracle
* GenerationType.TABLE--通用,但是效率不高
@Column标注实体的属性,对应数据库表中的字段,包括length,name,nullable等设置
@Temporal标注时间类型的属性,@Temporal(TemporalType.DATE),@Temporal(TemporalType.TIME),@Temporal(TemporalType.TIMESTAMP)
@Enumerated标注索引,包括@Enumerated(EnumType.ORDINAL)//保存枚举的索引值,@Enumerated(EnumType.STRING) //保存枚举的字面值(而非索引值)
@Lob //对应数据库大文本字段,还可以保存文件
@Transient //表示不与数据库映射
实例说明:
package com.yl.demo1.bean; import java.util.Date; import javax.persistence.Basic; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.EnumType; import javax.persistence.Enumerated; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Lob; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; import javax.persistence.Transient; @Entity @Table(name="t_person") public class Person { private Integer id; private String name; private Date birthday;//日期类型 private Sex sex = Sex.MAN;//枚举类型,并设置默认值 private String info; private String notinvoke;//不希望映射到数据库的字段 private Byte[] bigfile; public Person(){} public Person(String name) { this.name = name; } @Id @GeneratedValue public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(length=10, nullable=false, name="personName") public String getName() { return name; } public void setName(String name) { this.name = name; } @Temporal(TemporalType.DATE) public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Enumerated(EnumType.STRING) //保存枚举的字面值(而非索引值) @Column(length=5, nullable=false) public Sex getSex() { return sex; } public void setSex(Sex sex) { this.sex = sex; } @Lob //对应数据库大文本字段,还可以保存文件 public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } @Transient //表示不与数据库映射 public String getNotinvoke() { return notinvoke; } public void setNotinvoke(String notinvoke) { this.notinvoke = notinvoke; } /*** * FetchType.EAGER---立即加载 * FetchType.LAZY----延迟加载,没有访问get方法时不会加载 */ @Lob @Basic(fetch=FetchType.EAGER) public Byte[] getBigfile() { return bigfile; } public void setBigfile(Byte[] bigfile) { this.bigfile = bigfile; } }
上面类中用到的枚举
public enum Sex { MAN, WOMAN }
对实体的各种操作行为,包括保存,查询,更新,删除等。
1 @Test public void save() { 2 EntityManagerFactory factory = Persistence.createEntityManagerFactory("YL"); 3 EntityManager em = factory.createEntityManager(); 4 em.getTransaction().begin();//事务开始 5 Person p = new Person("老子"); 6 em.persist(p); 7 em.getTransaction().commit(); 8 em.close(); 9 factory.close(); 10 } 11 12 @Test public void getPerson() { 13 EntityManagerFactory factory = Persistence.createEntityManagerFactory("YL"); 14 EntityManager em = factory.createEntityManager(); 15 Person person = em.find(Person.class, 1);//相当于hibernate的get方法 16 /** 17 * 这里执行了一个占时较长的任务时,其他部分可能修改了数据库中的对象,如果下面想要继续使用这个对象,可以使用refresh方法获取最新的数据 18 *此时调用查询方法只能是在EntityManager的一级缓存中获取刚才查询到的数据,无法获取最新的数据 19 */ 20 em.refresh(person); 21 System.out.println(person.getName()); 22 em.close(); 23 factory.close(); 24 } 25 26 @Test public void getPerson2() { 27 EntityManagerFactory factory = Persistence.createEntityManagerFactory("JPA"); 28 EntityManager em = factory.createEntityManager(); 29 30 Person person = em.getReference(Person.class, 1);//相当于hibernate的load方法,延迟加载 31 System.out.println(person.getName()); 32 33 em.close(); 34 //System.out.println(person.getName());//获取出错 35 factory.close(); 36 } 37 /**实体的状态 38 * new 39 * 托管 40 * 游离 41 * 删除 42 * 43 */ 44 45 @Test public void updatePerson() { 46 EntityManagerFactory factory = Persistence.createEntityManagerFactory("JPA"); 47 EntityManager em = factory.createEntityManager(); 48 em.getTransaction().begin();//事务开始 49 Person person = em.find(Person.class, 1); 50 person.setName("孔子"); 51 em.getTransaction().commit(); 52 em.close(); 53 factory.close(); 54 } 55 56 57 @Test public void updatePerson2() { 58 EntityManagerFactory factory = Persistence.createEntityManagerFactory("JPA"); 59 EntityManager em = factory.createEntityManager(); 60 Person person = em.find(Person.class, 1); 61 em.clear();//把实体管理器中的所有实体变成游离状态 62 person.setName("lao jia"); 63 em.merge(person);//把游离状态的实体同步到实体管理器 64 em.close(); 65 //person.getName();//此时无法查询到数据 66 factory.close(); 67 } 68 69 70 @Test public void deletePerson() { 71 EntityManagerFactory factory = Persistence.createEntityManagerFactory("JPA"); 72 EntityManager em = factory.createEntityManager(); 73 em.getTransaction().begin();//事务开始 74 Person person = em.find(Person.class, 1); 75 em.remove(person); 76 em.getTransaction().commit(); 77 em.close(); 78 factory.close(); 79 } 80 81 @Test public void query() { 82 EntityManagerFactory factory = Persistence.createEntityManagerFactory("JPA"); 83 EntityManager em = factory.createEntityManager(); 84 Query query = em.createQuery("select o from Person o where o.id = ?1");//?后指定参数的索引 85 query.setParameter(1, 2);//1代表参数的索引,2代表相应的值,在这里即id=2 86 Person person = (Person) query.getSingleResult(); 87 System.out.println(person.getName()); 88 em.close(); 89 factory.close(); 90 } 91 92 @Test public void deleteQuery() { 93 EntityManagerFactory factory = Persistence.createEntityManagerFactory("JPA"); 94 EntityManager em = factory.createEntityManager(); 95 em.getTransaction().begin();//事务开始 96 Query query = em.createQuery("delete from Person o where o.id = ?1");//?后指定参数的索引 97 query.setParameter(1, 2);//1代表参数的索引,2代表相应的值,在这里即id=2 98 query.executeUpdate(); 99 em.getTransaction().commit(); 100 em.close(); 101 factory.close(); 102 } 103 104 @Test public void updateQuery() { 105 EntityManagerFactory factory = Persistence.createEntityManagerFactory("JPA"); 106 EntityManager em = factory.createEntityManager(); 107 em.getTransaction().begin();//事务开始 108 Query query = em.createQuery("update Person o set o.name = :name and o.id = :id");//?后指定参数的索引 109 query.setParameter(1, 2);//1代表参数的索引,2代表相应的值,在这里即id=2 110 query.executeUpdate(); 111 em.getTransaction().commit(); 112 em.close(); 113 factory.close(); 114 }
标签:style blog io color ar os 使用 java sp
原文地址:http://www.cnblogs.com/dreamfree/p/4077061.html