package junit.test;
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();
em.getTransaction().begin();
em.persist(new Person("中文"));
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);
em.refresh(person);//若直接更改了数据库中的数据,应该用refresh刷新一下数据库的动态变化,保证输出正确结果
System.out.println(person.getName());
em.close();
factory.close();
}
@Test public void getPerson2(){
EntityManagerFactory factory=Persistence.createEntityManagerFactory("itcast");
EntityManager em=factory.createEntityManager();
/**getReference方法返回一个代理对象,只有对该代理对象的属性访问时,才会从数据库中得到该条记录(即发生数据的加载行为)**/
Person person=em.getReference(Person.class,1);
/**若查找不到id为1的记录,会在对该代理对象的属性访问时,抛出异常。**/
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 deletePerson(){
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();
}
/**JPQL语言**/
@Test public void queryPerson(){
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, 1);
//Person person=(Person)query.getSingleResult();
// System.out.println(person.getName());
List<Person> persons=query.getResultList();
for(Person person:persons)
{
System.out.println(person.getName());
}
em.close();
factory.close();
}
@Test public void delete(){
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,1);
query.executeUpdate();
em.getTransaction().commit();
em.close();
factory.close();
}
@Test public void update(){
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",2);
query.executeUpdate();
em.getTransaction().commit();
em.close();
factory.close();
}
}
- 1
- 2
前往页