JPQL照会等の照会方法

8193 ワード

package zhang.jpa.test;

import java.util.Arrays;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

import org.hibernate.ejb.QueryHints;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import zhang.jpa.helloword.Department;
import zhang.jpa.helloword.Employee;
import zhang.jpa.helloword.Item;
import zhang.jpa.helloword.Manager;
import zhang.jpa.helloword.Type;
import zhang.jpa.helloword.User;

public class Testonetoone {
	private EntityManagerFactory entityManagerFactory = null;
	private EntityManager entityManager = null;
	private EntityTransaction transaction = null;

	@Before
	public void init() {
		String persistenceUnitName = "jpa-1";
		entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName);
		entityManager = entityManagerFactory.createEntityManager();
		transaction = entityManager.getTransaction();
		transaction.begin();
	}

	@After
	public void after() {
		transaction.commit();
		entityManager.close();
		entityManagerFactory.close();
	}
	//  
	@Test
	public void testjpqlupdate(){
		String jpql="update Employee set name=? where id=?";
		entityManager.createQuery(jpql).setParameter(1, "java").setParameter(2, 2).executeUpdate();
	}
	//  
	@Test
	public  void testjpqldelete(){
		String jpql="delete from Employee e where e.id=?";
		entityManager.createQuery(jpql).setParameter(1, 1).executeUpdate();
	}
	@Test
	public void testjpqlfunction(){
		String jpql="select upper(e.name),upper(e.email),email from Employee e";
		List list = entityManager.createQuery(jpql).getResultList();
		for(Object[] objs: list){
			System.out.println(Arrays.asList(objs));
		}
	}
	// jpql          
	@Test
	public void jpqlSubquery(){
		//  name AA      
		String sql="select e from Employee e where department="
				+ " (select d from Department d where d.departmentId=?)";
		Query query= entityManager.createQuery(sql).setParameter(1, 1);
		List list = query.getResultList();
		System.out.println(list);
		
	}

	//     ,  left outer join fetch,           ,                   
	//     fetch  ,    List
	@Test
	public void testleftjoinfetch() {
		String jpql = "from Department d left outer join fetch d.employees where d.departmentId=? ";
		Department department = (Department) entityManager.createQuery(jpql).setParameter(1, 1).getSingleResult();
		System.out.println(department.getDepartmentName());
		System.out.println(department.getEmployees().size());

		// String jpql2="from Department d left outer join d.employees where
		// d.departmentId=? ";
		// List list=entityManager.createQuery(jpql2).setParameter(1,
		// 1).getResultList();
		// for(Object[] objs:list){
		// System.out.println((Employee)(Arrays.asList(objs).get(1)));
		// }

	}

	//     group by,count,avg     
	@Test
	public void testgroupby() {
		String jpql = "select count(*) from User group by id ";
		List list = entityManager.createQuery(jpql).getResultList();
		System.out.println(list);
	}

	//  jpql     order by
	@Test
	public void testorderby() {
		String jpql = "from User where id=1 order by id desc";
		User user = (User) entityManager.createQuery(jpql).getSingleResult();
		System.out.println(user);
	}

	//       
	@Test
	public void querycache() {
		String jpql = "from User u where u.id=?";
		Query query = entityManager.createQuery(jpql).setHint(QueryHints.HINT_CACHEABLE, true).setParameter(1, 1);
		User user = (User) query.getSingleResult();

		query = entityManager.createQuery(jpql).setHint(QueryHints.HINT_CACHEABLE, true).setParameter(1, 1);
		user = (User) query.getSingleResult();

	}

	//   sql
	@Test
	public void testnativesql() {
		String jpql = "select last_name from jpa_user where id=1";
		List list = entityManager.createNativeQuery(jpql).getResultList();
		System.out.println(list);
	}

	@Test
	public void testnamedquery() {
		Query query = entityManager.createNamedQuery("testnamedquery").setParameter("id", 3);
		List emails = query.getResultList();
		System.out.println(emails);
	}

	//       ,              
	@Test
	public void testgetfields() {
		String jpql = "select new User(username,email) from User u where u.id>:id";
		Query query = entityManager.createQuery(jpql).setParameter("id", 5);
		// List list = query.getResultList();
		List users = query.getResultList();
		for (User user : users) {
			System.out.println(user);
		}
	}

	//             
	@Test
	public void testhellojpql() {
		String jpql = "from User u where u.id=?";
		Query query = entityManager.createQuery(jpql).setParameter(1, 1);
		User user = (User) query.getSingleResult();
		System.out.println(user);
	}

	//     
	@Test
	public void testsecondlevelcache() {
		Employee employee1 = entityManager.find(Employee.class, 1);

		transaction.commit();
		entityManager.close();

		entityManager = entityManagerFactory.createEntityManager();
		transaction = entityManager.getTransaction();
		transaction.begin();

		Employee employee2 = entityManager.find(Employee.class, 1);
	}

	//     
	@Test
	public void testmanytomanydelete() {
		// Item item=entityManager.find(Item.class, 2);
		// entityManager.remove(item);
		Type type = entityManager.find(Type.class, 1);
		entityManager.remove(type);
	}

	//   
	@Test
	public void testupdateonetoone() {
		Item item = entityManager.find(Item.class, 1);
		System.out.println(item);
		item.setItemName("item_shoe");
		item.getTypes().iterator().next().setTypeName("mytype");
	}

	//             (           )(            )
	@Test
	public void testmanytomanyget2() {
		Type type = entityManager.find(Type.class, 1);
		System.out.println(type.getTypeName());
		System.out.println(type.getItems().size());
	}

	//             (           )
	@Test
	public void testmanytomanyget() {
		Item item = entityManager.find(Item.class, 1);
		System.out.println(item.getItemName());

		System.out.println(item.getTypes().size());
	}

	//           ,.          ,           ,                 
	@Test
	public void testmanytomanysave() {
		Item item = new Item();
		item.setItemName("item1");

		Item item2 = new Item();
		item2.setItemName("item2");

		Type type1 = new Type();
		type1.setTypeName("typeAA");

		Type type2 = new Type();
		type2.setTypeName("TypeBB");
		//       
		item.getTypes().add(type1);
		item.getTypes().add(type2);
		item2.getTypes().add(type1);
		item2.getTypes().add(type2);

		type1.getItems().add(item);
		type1.getItems().add(item2);
		type2.getItems().add(item2);
		type1.getItems().add(item);
		//     
		entityManager.persist(item);
		entityManager.persist(item2);
		entityManager.persist(type1);
		entityManager.persist(type2);

	}

	//                    ,       ,            
	//     @OneToOne(fetch=FetchType.LAZY),       ,       
	@Test
	public void testonetoonebothget2() {
		Department department = entityManager.find(Department.class, 1);
		System.out.println(department.getDepartmentName());
		System.out.println(department.getManager().getClass());
	}

	//                     ,       ,            
	@Test
	public void testonetoonebothget() {
		Manager manager = entityManager.find(Manager.class, 1);
		System.out.println(manager.getManagerName());
		System.out.println(manager.getDepartment().getDepartmentName());
	}

	@Test
	public void testonetooesave() {
		Department department = new Department();
		department.setDepartmentName("IT");

		Manager manager = new Manager();
		manager.setManagerName("zhangyukangmanager");

		department.setManager(manager);
		manager.setDepartment(department);

		entityManager.persist(manager);
		entityManager.persist(department);
	}

	@Test
	public void testsaveemployee() {
		Employee employee = new Employee("AA", 45, "[email protected]", "13525", entityManager.find(Department.class, 1));
		entityManager.persist(employee);
	}

}