JPA下のオリジナルsqlクエリー

3563 ワード

private @Autowired EntityManager em;
//private @PersistenceContext EntityManager defaultEm;
@SuppressWarnings("unchecked")
public List findByName1(String bookName, String expressName) {
	String sql = "select * from t_book b left join t_express e on b.exp_id = e.id where "
			+ "b.name like '" + bookName + "' and e.name like '" + expressName +"'";
	Query query = em.createNativeQuery(sql, Book.class);
	//query  executeUpdate、getFirstResult   ,    sql  
	List bookList = query.getResultList();
	return bookList;
}
//@Query(value="select * from t_book b left join t_express e on b.exp_id = e.id where "
//			+ "b.name like ?1 and e.name like ?2", nativeQuery=true)
//List findByName(String bookName, String expressName);	
public List findByName2(String bookName, String expressName) {
	return this.repository.findByName(bookName, expressName);
}

@SuppressWarnings("unchecked")
public List findBookExpress1(String bookName, String expressName) {
	String sql = "select b.id as bookId,b.name as bookName, e.id as expressId, e.name as expressName from t_book b left join t_express e "
			+ "on b.exp_id = e.id where b.name like '" + bookName + "' and e.name like '" + expressName +"'";
	//         ResultTransformer   , Hibernate                    Object   。
	//        ,       addScalar
	Query query = em.createNativeQuery(sql);
	List result = query.unwrap(SQLQuery.class)
			.addScalar("bookId", LongType.INSTANCE).addScalar("bookName").addScalar("expressId", LongType.INSTANCE).addScalar("expressName")
			.setResultTransformer(Transformers.aliasToBean(BookExpress.class)).list();
	return result;
}

//  sql        
@SuppressWarnings("unchecked")
public List findBookExpress2(String bookName, String expressName) throws IllegalAccessException, InvocationTargetException {
	String sql = "select b.id as \"bookId\",b.name as \"bookName\", e.id as \"expressId\", e.name as \"expressName\" from t_book b left join t_express e "
			+ "on b.exp_id = e.id where b.name like '" + bookName + "' and e.name like '" + expressName +"'";
	Query query = em.createNativeQuery(sql);
	List> resultList = query.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
	if(CollectionUtils.isEmpty(resultList)) {
		return null;
	}
	List result = new ArrayList<>(resultList.size());
	for(Map map : resultList) {
		BookExpress exp = new BookExpress();
		BeanUtils.populate(exp, map);
		result.add(exp);
	}
	return result;
}
//getDelegate         session,  dowork          (org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl@10cf3d8 is closed)
public List findBookExpress3(String bookName, String expressName) {
	Session session = (Session)em.getDelegate();//   getDelegate     ,unwrap    
	//Session session = em.unwrap(Session.class);
	BookExpressQueryWork1 bookExpressQueryWork = new BookExpressQueryWork1();
	bookExpressQueryWork.setBookName(bookName);
	bookExpressQueryWork.setExpressName(expressName);
	
	session.doWork(bookExpressQueryWork);
	session.disconnect();
	return bookExpressQueryWork.getBookExpresses();
}