package junit.test;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.junit.BeforeClass;
import org.junit.Test;
import cn.itcast.bean.Person;
public class PersonTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test public void save() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("itcast");
EntityManager em = factory.createEntityManager();
// -->sessionFactory-->session-->begin事务
em.getTransaction().begin();
em.persist(new Person("传智播客", new Date()));
em.getTransaction().commit();
em.close();
factory.close();
}
@Test public void getPerson() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("itcast");
EntityManager em = factory.createEntityManager();
Person person = em.find(Person.class, 1); // get()
System.out.println(person.getName());
em.close();
factory.close();
}
@Test public void getPerson2() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("itcast");
EntityManager em = factory.createEntityManager();
Person person = em.getReference(Person.class, 3); // load()
System.out.println(person.getName());
em.close();
factory.close();
}
@Test public void updatePerson() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("itcast");
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Person person = em.find(Person.class, 1);
person.setName("老张");
em.getTransaction().commit();
em.close();
factory.close();
}
@Test public void updatePerson2() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("itcast");
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Person person = em.find(Person.class, 1);
em.clear(); //把实体变成游离状态
person.setName("老王");
em.merge(person); //用于把游离状态的对象更新同步到数据库
em.getTransaction().commit();
em.close();
factory.close();
}
@Test public void delete() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("itcast");
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Person person = em.find(Person.class, 1);
em.remove(person); //把托管状态的实体删掉
em.getTransaction().commit();
em.close();
factory.close();
}
@Test public void query() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("itcast");
EntityManager em = factory.createEntityManager();
Query query = em.createQuery("select o from Person o where o.id=?1");
query.setParameter(1, 2);
Person person = (Person) query.getSingleResult();
System.out.println(person.getName());
em.close();
factory.close();
}
@Test public void deletequery() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("itcast");
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Query query = em.createQuery("delete from Person o where o.id=?1");
query.setParameter(1, 2);
query.executeUpdate();
em.getTransaction().commit();
em.close();
factory.close();
}
@Test public void updatequery() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("itcast");
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Query query = em.createQuery("update Person o set o.name=:name where o.id=:id");
query.setParameter("name", "xxx");
query.setParameter("id", 3);
query.executeUpdate();
em.getTransaction().commit();
em.close();
factory.close();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
JPA用于整合现有的ORM技术,可以简化现有Java EE和Java SE应用的对象持久化的开发工作,实现ORM的统一。JPA作为一项对象持久化的标准,不但可以获得Java EE应用服务器的支持,还可以直接在Java SE中使用。JPA必将成为Java持久化解决方案的主流,如果你是Hibernate或者TopLink的等ORM技术的忠实用户,不管你是否情愿,你迟早也得使用JPA,那现在正好可以借助传智播客视频教程的帮助,开始学习和尝试JPA吧!
资源推荐
资源详情
资源评论
收起资源包目录
JPA.rar (44个子文件)
JPA
hibernate-cglib-repack-2.1_3.jar 377KB
javassist-3.4.GA.jar 460KB
.project 379B
hibernate-commons-annotations.jar 65KB
oro-2.0.8.jar 64KB
commons-fileupload-1.1.1.jar 31KB
commons-io-1.1.jar 60KB
slf4j-log4j12.jar 8KB
dom4j-1.6.1.jar 307KB
commons-pool.jar 61KB
cglib-nodep-2.1_3.jar 317KB
src
cn
itcast
bean
Gender.java 65B
Person.java 2KB
META-INF
persistence.xml 1KB
junit
test
PersonTest.java 4KB
bsf-2.3.0.jar 171KB
commons-beanutils-1.7.0.jar 184KB
commons-dbcp.jar 119KB
antlr-2.7.6.jar 433KB
commons-collections-3.1.jar 546KB
commons-digester-1.8.jar 140KB
.settings
org.eclipse.jdt.core.prefs 629B
org.eclipse.core.resources.prefs 88B
commons-validator-1.3.1.jar 136KB
commons-chain-1.1.jar 88KB
aspectjweaver.jar 1.47MB
commons-logging.jar 59KB
ehcache-1.2.3.jar 203KB
log4j-1.2.15.jar 383KB
hibernate3.jar 2.64MB
ejb3-persistence.jar 49KB
common-annotations.jar 6KB
aspectjrt.jar 112KB
hibernate-annotations.jar 273KB
hibernate-entitymanager.jar 116KB
.classpath 2KB
jta-1.1.jar 13KB
slf4j-api-1.5.2.jar 17KB
commons-logging-1.0.4.jar 37KB
bin
cn
itcast
bean
Gender.class 962B
Person.class 3KB
META-INF
persistence.xml 1KB
junit
test
PersonTest.class 4KB
mysql-connector-java-3.1.13-bin.jar 446KB
共 44 条
- 1
showershow
- 粉丝: 1
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页