Spring Data JPA书写原生态sql拼接

package com.chinasofti.vtcsvc.persist.primary.repo;


import java.util.List;


import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


import com.chinasofti.vtcsvc.persist.primary.bean.VtmProfilePO;
import com.chinasofti.vtcsvc.persist.primary.model.VtmProfileDO;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:chinasofti/vtcsvc/persist/applicationContext-persist.xml")


public class test {


@Autowired
private LocalContainerEntityManagerFactoryBean entityManagerFactory;


/**
* 查询的结果是实体的集合
*/
@Test
public void testNativeQuery2() {
String sql = "SELECT v.id,v.code,v.location FROM vtmprofile v";

EntityManager em = entityManagerFactory.getNativeEntityManagerFactory().createEntityManager();
Query query = em.createNativeQuery(sql, VtmProfileDO.class);
@SuppressWarnings({ "unused", "unchecked" })
List userList = query.getResultList();
if (em != null) {
em.close();
}
}


}

你可能感兴趣的:(Spring Data JPA书写原生态sql拼接)