怎样用JPA的EntityManager执行原生sql返回ResultSet-CSDN论坛
1个回答
展开全部
试:
[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";
[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";
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询