spring data jpa 想使用EntityManager 对sql 进行处理四种方式(第四种本人改写的)

下面看看主体的一个类:

package com.chinait.service.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.websocket.server.ServerEndpoint;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Service;

import com.chinait.service.TestService;
import com.chinait.utils.Testtrans;
import com.chinait.vo.Test;

@Service
public class TestServiceImpl implements TestService{
	@PersistenceContext
	private EntityManager entityManager;
	@Override	
	public void getTest(){
		String sql = "select apps.appName,source.cutPath,sourcetype.sourceTypeName from apps join source on source.apps_id = apps.id join sourcetype on sourcetype.id = source.sourceType_id";
		Session session = entityManager.unwrap(org.hibernate.Session.class);
		//返回类型是List
		SQLQuery query2 = session.createSQLQuery(sql);
		query2.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		System.out.println("查询数据返回map"+query2.list());
		//返回数据是list>
		SQLQuery query3 = session.createSQLQuery(sql);
		query2.setResultTransformer(Transformers.TO_LIST);
		System.out.println("查询数据返回map"+query2.list());
		//返回类型是可定制化对象
		SQLQuery query = session.createSQLQuery(sql);
		Testtrans Testtrans = new Testtrans(Test.class);
		query.setResultTransformer(Testtrans);
		List list = (List)query.list();
		System.out.println("查询数据返回对象"+list);
		//返回数据是对象,首先这个方法有点坑,因为它需要去找寻bean 对应的对象是否是放在spring data jpa 的entity 如果不是的话回报错
		/*SQLQuery query4 = session.createSQLQuery(sql);
		query.setResultTransformer(Transformers.aliasToBean(Test.class));
		List list3 = (List)query.list();
		System.out.println("查询数据返回对象"+list);*/
	}
}
下面则是:我重写的一个类的源码:

package com.chinait.utils;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.transform.AliasToBeanResultTransformer;


public class Testtrans extends AliasToBeanResultTransformer{
	private Class resultClass;
	public Testtrans(Class resultClass) {
		super(resultClass);
		this.resultClass = resultClass;
	}

	private static final long serialVersionUID = 1L;

	public List transformTuple(Object[] tuple, String[] aliases) {
		  List list = new ArrayList();
		  Object obj = null;
			try {
				obj = resultClass.newInstance();
			} catch (InstantiationException e1) {
				e1.printStackTrace();
			} catch (IllegalAccessException e1) {
				e1.printStackTrace();
			}
		  Method[] methods = resultClass.getMethods();// 返回这个类里面方法的集合  
		  for(int k=0;k
注意Test 的实体类:

package com.chinait.vo;
//apps.appName,source.cutPath,sourcetype.sourceTypeName
public class Test {
	private String appName;
	private String cutPath;
	private String sourceTypeName;
	public String getAppName() {
		return appName;
	}
	public void setAppName(String appName) {
		this.appName = appName;
	}
	public String getCutPath() {
		return cutPath;
	}
	public void setCutPath(String cutPath) {
		this.cutPath = cutPath;
	}
	public String getSourceTypeName() {
		return sourceTypeName;
	}
	public void setSourceTypeName(String sourceTypeName) {
		this.sourceTypeName = sourceTypeName;
	}
	@Override
	public String toString() {
		return "Test [appName=" + appName + ", cutPath=" + cutPath + ", sourceTypeName=" + sourceTypeName + "]";
	}
}

输出结果:

spring data jpa 想使用EntityManager 对sql 进行处理四种方式(第四种本人改写的)_第1张图片

*注意这里还有一个bug,那就是select * 这里会报错,因为sqlquery 是按照字段的别名来判断数据与实体的对应关系。所以这里如果写成是select * 回报一个异常是指无法匹配

这边有兴趣的就是看这个源码。spring data jpa 想使用entityManager 对sql 进行处理四种方式(第四种本人改写的)。

你可能感兴趣的:(java,ee,框架使用.)