怎样用JPA的EntityManager执行原生sql返回ResultSet-CSDN论坛
发布网友
发布时间:2022-04-26 00:18
我来回答
共1个回答
热心网友
时间:2022-04-10 12:51
试:
[java] view plaincopy
package com.cndatacom.jpa.test;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.cndatacom.jpa.entity.User;
/**
* 测试JPA原生SQL查询
* @author Luxh
*/
public class TestNativeQuery {
EntityManagerFactory emf = null;
@Before
public void before() {
//根据在persistence.xml中配置的persistence-unit name 创建EntityManagerFactory
emf = Persistence.createEntityManagerFactory("myJPA");
}
@After
public void after() {
//关闭EntityManagerFactory
if(null != emf) {
emf.close();
}
}
/**
* 查询的结果是对象数组的集合
*/
@Test
public void testNativeQuery1() {
EntityManager em = emf.createEntityManager();
//定义SQL
String sql = "SELECT * FROM t_user";