本文整理了Java中javax.persistence.EntityManager.createQuery()
方法的一些代码示例,展示了EntityManager.createQuery()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。EntityManager.createQuery()
方法的具体详情如下:
包路径:javax.persistence.EntityManager
类名称:EntityManager
方法名:createQuery
[英]Create an instance of Query
for executing a Java Persistence query language statement.
[中]创建Query
的实例以执行Java持久性查询语言语句。
代码示例来源:origin: spring-projects/spring-framework
@Test
public void testEntityManagerFactory2() {
EntityManager em = this.entityManagerFactory2.createEntityManager();
try {
em.createQuery("select tb from TestBean");
fail("Should have thrown IllegalArgumentException");
}
catch (IllegalArgumentException ex) {
// expected
}
finally {
em.close();
}
}
代码示例来源:origin: mitreid-connect/OpenID-Connect-Java-Spring-Server
@Override
@Transactional(value="defaultTransactionManager")
public void clearDuplicateAccessTokens() {
Query query = manager.createQuery("select a.jwt, count(1) as c from OAuth2AccessTokenEntity a GROUP BY a.jwt HAVING count(1) > 1");
@SuppressWarnings("unchecked")
List<Object[]> resultList = query.getResultList();
List<JWT> values = new ArrayList<>();
for (Object[] r : resultList) {
logger.warn("Found duplicate access tokens: {}, {}", ((JWT)r[0]).serialize(), r[1]);
values.add((JWT) r[0]);
}
if (values.size() > 0) {
CriteriaBuilder cb = manager.getCriteriaBuilder();
CriteriaDelete<OAuth2AccessTokenEntity> criteriaDelete = cb.createCriteriaDelete(OAuth2AccessTokenEntity.class);
Root<OAuth2AccessTokenEntity> root = criteriaDelete.from(OAuth2AccessTokenEntity.class);
criteriaDelete.where(root.get("jwt").in(values));
int result = manager.createQuery(criteriaDelete).executeUpdate();
logger.warn("Deleted {} duplicate access tokens", result);
}
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void deferredQueryWithResultList() {
EntityManagerFactory emf = mock(EntityManagerFactory.class);
EntityManager targetEm = mock(EntityManager.class);
Query query = mock(Query.class);
given(emf.createEntityManager()).willReturn(targetEm);
given(targetEm.createQuery("x")).willReturn(query);
given(targetEm.isOpen()).willReturn(true);
EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
em.createQuery("x").getResultList();
verify(query).getResultList();
verify(targetEm).close();
}
代码示例来源:origin: Impetus/Kundera
/**
* Test field with in clause.
*/
public void testFieldWithInClause()
{
String queryString = "Select p.personName from PersonES p where p.personId IN ( '1', '2') and p.age = 10";
Query query = em.createQuery(queryString);
List resultList = query.getResultList();
Assert.assertNotNull(resultList);
Assert.assertEquals(1, resultList.size());
Assert.assertEquals("karthik", resultList.get(0));
}
代码示例来源:origin: Impetus/Kundera
private void findByNameAndAgeWithOrClause()
{
EntityManager em;
String query;
Query q;
List<StudentCouchDBDate> students;
int count;
em = emf.createEntityManager();
query = "Select s From StudentOracleNoSQLDate s where s.name = Kuldeep and s.age > " + getMinValue(short.class);
q = em.createQuery(query);
students = q.getResultList();
Assert.assertNotNull(students);
Assert.assertEquals(1, students.size());
count = 0;
for (StudentCouchDBDate student : students)
{
Assert.assertEquals(getMaxValue(Date.class), student.getId());
Assert.assertEquals(getMaxValue(short.class), student.getAge());
Assert.assertEquals("Kuldeep", student.getName());
count++;
}
Assert.assertEquals(1, count);
em.close();
}
代码示例来源:origin: kiegroup/jbpm
@SuppressWarnings("unchecked")
public static ArrayList<ProcessInstanceInfo> retrieveProcessInstanceInfo(EntityManagerFactory emf) {
JtaTransactionManager txm = new JtaTransactionManager(null, null, null);
boolean txOwner = txm.begin();
EntityManager em = emf.createEntityManager();
ArrayList<ProcessInstanceInfo> procInstInfoList = new ArrayList<ProcessInstanceInfo>();
List<Object> mdList = em.createQuery("SELECT p FROM ProcessInstanceInfo p").getResultList();
for( Object resultObject : mdList ) {
ProcessInstanceInfo procInstInfo = (ProcessInstanceInfo) resultObject;
procInstInfoList.add(procInstInfo);
logger.trace("> {}", procInstInfo);
}
txm.commit(txOwner);
return procInstInfoList;
}
代码示例来源:origin: Impetus/Kundera
private <E extends Object> void assertFindByName(EntityManager em, String clazz, E e, String name,
String fieldName)
{
String query = "Select p from PesonMongo p where p." + fieldName + " = " + name;
// // find by name.
Query q = em.createQuery(query);
List<E> results = q.getResultList();
Assert.assertNotNull(results);
Assert.assertFalse(results.isEmpty());
Assert.assertEquals(3, results.size());
}
代码示例来源:origin: spring-projects/spring-framework
@Test
@SuppressWarnings("unchecked")
public void testQueryNoPersons() {
EntityManager em = entityManagerFactory.createEntityManager();
Query q = em.createQuery("select p from Person as p");
List<Person> people = q.getResultList();
assertEquals(0, people.size());
try {
assertNull(q.getSingleResult());
fail("Should have thrown NoResultException");
}
catch (NoResultException ex) {
// expected
}
}
代码示例来源:origin: Impetus/Kundera
private void findByNameAndAgeWithOrClause()
{
EntityManager em;
String query;
Query q;
List<StudentHBaseByteWrapper> students;
int count;
em = emf.createEntityManager();
query = "Select s From StudentHBaseByteWrapper s where s.name = Kuldeep and s.age > " + getPartialValue(short.class);
q = em.createQuery(query);
students = q.getResultList();
Assert.assertNotNull(students);
Assert.assertEquals(1, students.size());
count = 0;
for (StudentHBaseByteWrapper student : students)
{
Assert.assertEquals(getMaxValue(Byte.class), student.getId());
Assert.assertEquals(getMaxValue(short.class), student.getAge());
Assert.assertEquals("Kuldeep", student.getName());
count++;
}
Assert.assertEquals(1, count);
em.close();
}
代码示例来源:origin: mitreid-connect/OpenID-Connect-Java-Spring-Server
@Override
@Transactional(value="defaultTransactionManager")
public void clearDuplicateRefreshTokens() {
Query query = manager.createQuery("select a.jwt, count(1) as c from OAuth2RefreshTokenEntity a GROUP BY a.jwt HAVING count(1) > 1");
@SuppressWarnings("unchecked")
List<Object[]> resultList = query.getResultList();
List<JWT> values = new ArrayList<>();
for (Object[] r : resultList) {
logger.warn("Found duplicate refresh tokens: {}, {}", ((JWT)r[0]).serialize(), r[1]);
values.add((JWT) r[0]);
}
if (values.size() > 0) {
CriteriaBuilder cb = manager.getCriteriaBuilder();
CriteriaDelete<OAuth2RefreshTokenEntity> criteriaDelete = cb.createCriteriaDelete(OAuth2RefreshTokenEntity.class);
Root<OAuth2RefreshTokenEntity> root = criteriaDelete.from(OAuth2RefreshTokenEntity.class);
criteriaDelete.where(root.get("jwt").in(values));
int result = manager.createQuery(criteriaDelete).executeUpdate();
logger.warn("Deleted {} duplicate refresh tokens", result);
}
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void deferredQueryWithSingleResult() {
EntityManagerFactory emf = mock(EntityManagerFactory.class);
EntityManager targetEm = mock(EntityManager.class);
Query query = mock(Query.class);
given(emf.createEntityManager()).willReturn(targetEm);
given(targetEm.createQuery("x")).willReturn(query);
given(targetEm.isOpen()).willReturn(true);
EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
em.createQuery("x").getSingleResult();
verify(query).getSingleResult();
verify(targetEm).close();
}
代码示例来源:origin: Impetus/Kundera
private <E extends Object> void assertFindByName(EntityManager em, String clazz, E e, String name, String fieldName)
{
String query = "Select p from " + clazz + " p where p." + fieldName + " = " + name;
// // find by name.
Query q = em.createQuery(query);
List<E> results = q.getResultList();
Assert.assertNotNull(results);
Assert.assertFalse(results.isEmpty());
Assert.assertEquals(3, results.size());
}
代码示例来源:origin: spring-projects/spring-framework
@Test
@SuppressWarnings("unchecked")
public void testEntityManagerProxyIsProxy() {
EntityManager em = entityManagerFactory.createEntityManager();
assertTrue(Proxy.isProxyClass(em.getClass()));
Query q = em.createQuery("select p from Person as p");
List<Person> people = q.getResultList();
assertNotNull(people);
assertTrue("Should be open to start with", em.isOpen());
em.close();
assertFalse("Close should work on application managed EM", em.isOpen());
}
代码示例来源:origin: Impetus/Kundera
private void findByNameAndAgeWithOrClause()
{
EntityManager em;
String query;
Query q;
List<StudentHBaseInt> students;
int count;
em = emf.createEntityManager();
query = "Select s From StudentHBaseInt s where s.name = Kuldeep and s.age > " + getPartialValue(short.class);
q = em.createQuery(query);
students = q.getResultList();
Assert.assertNotNull(students);
Assert.assertEquals(1, students.size());
count = 0;
for (StudentHBaseInt student : students)
{
Assert.assertEquals(getMaxValue(int.class), student.getId());
Assert.assertEquals(getMaxValue(short.class), student.getAge());
Assert.assertEquals("Kuldeep", student.getName());
count++;
}
Assert.assertEquals(1, count);
em.close();
}
代码示例来源:origin: spring-projects/spring-framework
@Test
@SuppressWarnings("unchecked")
public void testMultipleResults() {
// Add with JDBC
String firstName = "Tony";
insertPerson(firstName);
assertTrue(Proxy.isProxyClass(sharedEntityManager.getClass()));
Query q = sharedEntityManager.createQuery("select p from Person as p");
List<Person> people = q.getResultList();
assertEquals(1, people.size());
assertEquals(firstName, people.get(0).getFirstName());
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void deferredQueryWithUpdate() {
EntityManagerFactory emf = mock(EntityManagerFactory.class);
EntityManager targetEm = mock(EntityManager.class);
Query query = mock(Query.class);
given(emf.createEntityManager()).willReturn(targetEm);
given(targetEm.createQuery("x")).willReturn(query);
given(targetEm.isOpen()).willReturn(true);
EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
em.createQuery("x").executeUpdate();
verify(query).executeUpdate();
verify(targetEm).close();
}
代码示例来源:origin: Impetus/Kundera
private <E extends Object> void assertFindByName(EntityManager em, String clazz, E e, String name, String fieldName)
{
String query = "Select p from PesonMongo p where p." + fieldName + " = " + name;
// // find by name.
Query q = em.createQuery(query);
List<E> results = q.getResultList();
Assert.assertNotNull(results);
Assert.assertFalse(results.isEmpty());
Assert.assertEquals(3, results.size());
}
代码示例来源:origin: spring-projects/spring-framework
@Test
@SuppressWarnings("unchecked")
public void testQueryNoPersonsNotTransactional() {
endTransaction();
EntityManager em = entityManagerFactory.createEntityManager();
Query q = em.createQuery("select p from Person as p");
List<Person> people = q.getResultList();
assertEquals(0, people.size());
try {
assertNull(q.getSingleResult());
fail("Should have thrown NoResultException");
}
catch (NoResultException ex) {
// expected
}
}
代码示例来源:origin: Impetus/Kundera
private void findByNameAndAgeWithOrClause()
{
EntityManager em;
String query;
Query q;
List<StudentMongoInt> students;
int count;
em = emf.createEntityManager();
query = "Select s From StudentMongoInt s where s.name = Kuldeep and s.age > " + getMinValue(short.class);
q = em.createQuery(query);
students = q.getResultList();
Assert.assertNotNull(students);
Assert.assertEquals(1, students.size());
count = 0;
for (StudentMongoInt student : students)
{
Assert.assertEquals(getMaxValue(int.class), student.getId());
Assert.assertEquals(getMaxValue(short.class), student.getAge());
Assert.assertEquals("Kuldeep", student.getName());
count++;
}
Assert.assertEquals(1, count);
em.close();
}
代码示例来源:origin: kiegroup/jbpm
private long timeQueryExecution(EntityManager em, CriteriaQuery query, Query realQuery, int total) {
if( realQuery == null ) {
realQuery = em.createQuery(query);
realQuery.setMaxResults(2000);
}
long start = System.nanoTime();
List<TaskSummary> results = realQuery.getResultList();
long end = System.nanoTime();
assertEquals( "query results", total, results.size() );
return (end - start)/1000000;
}
内容来源于网络,如有侵权,请联系作者删除!