Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JPA 3.2 features #1974

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ public class ExceptionLocalizationResource extends ListResourceBundle {
{ "getpersistenceunitutil_called_on_closed_emf", "getPersistenceUnitUtil() was called on a closed EntityManagerFactory."},
{ "named_entity_graph_exists", "NamedEntityGraph with name {0} found on {1} already exists in this persistence unit."},
{ "cannot_get_from_non_correlated_query", "getCorrelationParent() called on a from-clause that was not obtained through correlation." },
{ "no_key_in_entity", "Cannot add join of {0} to {1} because target class is missing attribute of source type"},
{ "RIGHT_JOIN_NOT_SUPPORTED", "Right join is not supported"},
{ "wrap_convert_exception", "An exception occurred while calling {0} on converter class {1} with value {2}"},
{ "ora_pessimistic_locking_with_rownum", "Pessimistic locking with query row limits is not supported."},
{ "bean_validation_constraint_violated", "One or more Bean Validation constraints were violated while executing Automatic Bean Validation on callback event: {0} for class: {1}. Please refer to the embedded constraint violations for details."},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,10 @@ public class LoggingLocalizationResource extends ListResourceBundle {
{ "dbws_orm_metadata_read_error", "The [{0}] ORM metadata could not be read."},
{ "dbws_oxm_metadata_read_error", "The [{0}] OXM metadata could not be read."},
{ "dbws_no_wsdl_inline_schema", "The [{0}] WSDL inline schema could not be read."},
// JPA 3.2
{ "unknown_property_type", "Unknown {0} type of {1} persistence property"},
{ "error_queryTimeoutParse", "Could not parse jakarta.persistence.query.timeout property value {0}: {1}"},

{ "validate_object_space", "validate object space." },
{ "stack_of_visited_objects_that_refer_to_the_corrupt_object", "stack of visited objects that refer to the corrupt object: {0}" },
{ "corrupt_object_referenced_through_mapping", "The following corrupt object is referenced through mapping: {0}" },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
/*
* Copyright (c) 1998, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/

// Contributors:
// 10/25/2023: Tomas Kraus
// - New Jakarta Persistence 3.2 Features
package org.eclipse.persistence.jpa.test.query.model;

import java.util.List;

import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.TypedQuery;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.ParameterExpression;
import jakarta.persistence.criteria.Root;
import org.eclipse.persistence.jpa.test.framework.DDLGen;
import org.eclipse.persistence.jpa.test.framework.Emf;
import org.eclipse.persistence.jpa.test.framework.EmfRunner;
import org.eclipse.persistence.jpa.test.framework.Property;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

@RunWith(EmfRunner.class)
public class TestQueryEqualTo {

@Emf(createTables = DDLGen.DROP_CREATE, classes = { EntityTbl01.class },
properties = { @Property(name="eclipselink.logging.level", value="FINE")})
private EntityManagerFactory emf;

private static boolean POPULATED = false;

@Test
public void testQueryEqualToExpression() {
if (!POPULATED)
populate();

EntityManager em = emf.createEntityManager();

try {
TypedQuery<String> query = em.createQuery(
"SELECT t.KeyString FROM EntityTbl01 t "
+ "WHERE t.KeyString = ?1", String.class);
query.setParameter(1, "EqualTo_1");
List<String> dto01 = query.getResultList();
assertNotNull(dto01);
assertEquals(1, dto01.size());

// equivalent CriteriaBuilder
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<String> cquery = cb.createQuery(String.class);
Root<EntityTbl01> root = cquery.from(EntityTbl01.class);
ParameterExpression<String> strParam1 = cb.parameter(String.class);
cquery.multiselect(root.get(EntityTbl01_.KeyString));
cquery.where(root.get(EntityTbl01_.KeyString).equalTo(strParam1));
query = em.createQuery(cquery);
query.setParameter(strParam1, "EqualTo_1");
dto01 = query.getResultList();
assertNotNull(dto01);
assertEquals(1, dto01.size());
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
if(em.isOpen()) {
em.close();
}
}
}

@Test
public void testQueryEqualToObject() {
if (!POPULATED)
populate();

EntityManager em = emf.createEntityManager();

try {
TypedQuery<String> query = em.createQuery(
"SELECT t.KeyString FROM EntityTbl01 t "
+ "WHERE t.KeyString = 'EqualTo_2'", String.class);
List<String> dto01 = query.getResultList();
assertNotNull(dto01);
assertEquals(1, dto01.size());

// equivalent CriteriaBuilder
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<String> cquery = cb.createQuery(String.class);
Root<EntityTbl01> root = cquery.from(EntityTbl01.class);
cquery.multiselect(root.get(EntityTbl01_.KeyString));
cquery.where(root.get(EntityTbl01_.KeyString).equalTo("EqualTo_2"));
query = em.createQuery(cquery);
dto01 = query.getResultList();
assertNotNull(dto01);
assertEquals(1, dto01.size());
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
if(em.isOpen()) {
em.close();
}
}
}

@Test
public void testQueryNotEqualToExpression() {
if (!POPULATED)
populate();

EntityManager em = emf.createEntityManager();

try {
TypedQuery<String> query = em.createQuery(
"SELECT t.KeyString FROM EntityTbl01 t "
+ "WHERE t.KeyString <> ?1", String.class);
query.setParameter(1, "EqualTo_3");
List<String> dto01 = query.getResultList();
assertNotNull(dto01);
assertTrue(dto01.size() > 1);

// equivalent CriteriaBuilder
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<String> cquery = cb.createQuery(String.class);
Root<EntityTbl01> root = cquery.from(EntityTbl01.class);
ParameterExpression<String> strParam1 = cb.parameter(String.class);
cquery.multiselect(root.get(EntityTbl01_.KeyString));
cquery.where(root.get(EntityTbl01_.KeyString).notEqualTo(strParam1));
query = em.createQuery(cquery);
query.setParameter(strParam1, "EqualTo_3");
dto01 = query.getResultList();
assertNotNull(dto01);
assertTrue(dto01.size() > 1);
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
if(em.isOpen()) {
em.close();
}
}
}

@Test
public void testQueryNotEqualToObject() {
if (!POPULATED)
populate();

EntityManager em = emf.createEntityManager();

try {
TypedQuery<String> query = em.createQuery(
"SELECT t.KeyString FROM EntityTbl01 t "
+ "WHERE t.KeyString <> 'EqualTo_4'", String.class);
List<String> dto01 = query.getResultList();
assertNotNull(dto01);
assertTrue(dto01.size() > 1);

// equivalent CriteriaBuilder
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<String> cquery = cb.createQuery(String.class);
Root<EntityTbl01> root = cquery.from(EntityTbl01.class);
cquery.multiselect(root.get(EntityTbl01_.KeyString));
cquery.where(root.get(EntityTbl01_.KeyString).notEqualTo("EqualTo_4"));
query = em.createQuery(cquery);
dto01 = query.getResultList();
assertNotNull(dto01);
assertTrue(dto01.size() > 1);
} finally {
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
if(em.isOpen()) {
em.close();
}
}
}

private void populate() {
EntityManager em = emf.createEntityManager();
try {
em.getTransaction().begin();

EntityTbl01 tbl1 = new EntityTbl01();
tbl1.setKeyString("EqualTo_1");
tbl1.setItemString1("A");
tbl1.setItemString2(null);
tbl1.setItemString3("C");
tbl1.setItemString4("D");
tbl1.setItemInteger1(45);
em.persist(tbl1);

EntityTbl01 tbl2 = new EntityTbl01();
tbl2.setKeyString("EqualTo_2");
tbl2.setItemString1("A");
tbl2.setItemString2("B");
tbl2.setItemString3("C");
tbl2.setItemString4(null);
tbl2.setItemInteger1(83);
em.persist(tbl2);

EntityTbl01 tbl3 = new EntityTbl01();
tbl3.setKeyString("EqualTo_3");
tbl3.setItemString1(null);
tbl3.setItemString2("B");
tbl3.setItemString3("C");
tbl3.setItemString4("D");
tbl3.setItemInteger1(17);
em.persist(tbl3);

EntityTbl01 tbl4 = new EntityTbl01();
tbl4.setKeyString("EqualTo_4");
tbl4.setItemString1("A");
tbl4.setItemString2("B");
tbl4.setItemString3("C");
tbl4.setItemString4(null);
tbl4.setItemInteger1(29);
em.persist(tbl4);

em.getTransaction().commit();

POPULATED = true;
} finally {
if(em.isOpen()) {
em.close();
}
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public static Test suite() {
suite.addTest(new CriteriaQueryMetamodelTest("testMetamodelOnClause"));
suite.addTest(new CriteriaQueryMetamodelTest("testMetamodelOnClauseOverCollection"));
suite.addTest(new CriteriaQueryMetamodelTest("testMetamodelOnClauseWithLeftJoin"));
suite.addTest(new CriteriaQueryMetamodelTest("testMetamodelOnClauseWithLeftJoinOnClass"));
suite.addTest(new CriteriaQueryMetamodelTest("simpleMetamodelCriteriaUpdateTest"));
suite.addTest(new CriteriaQueryMetamodelTest("testMetamodelCriteriaUpdate"));
suite.addTest(new CriteriaQueryMetamodelTest("testMetamodelComplexConditionCaseInCriteriaUpdate"));
Expand Down Expand Up @@ -181,6 +182,34 @@ public void testMetamodelOnClauseWithLeftJoin() {
}
}

// Join directly on Address class must return the same results as join on attribute
public void testMetamodelOnClauseWithLeftJoinOnClass() {
EntityManager em = createEntityManager();
Query query = em.createQuery("Select e from Employee e left join e.address a on a.city = 'Ottawa' " +
"where a.postalCode is not null");
List<?> baseResult = query.getResultList();

Metamodel metamodel = em.getMetamodel();
EntityType<Employee> entityEmp_ = metamodel.entity(Employee.class);
EntityType<Address> entityAddr_ = metamodel.entity(Address.class);

CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery<Employee>cq = qb.createQuery(Employee.class);
Root<Employee> root = cq.from(entityEmp_);
Join<Employee, Address> address = root.join(Address.class, JoinType.LEFT);
address.on(qb.equal(address.get(entityAddr_.getSingularAttribute("city", String.class)), "Ottawa"));
cq.where(qb.isNotNull(address.get(entityAddr_.getSingularAttribute("postalCode", String.class))));
List<?> testResult = em.createQuery(cq).getResultList();

clearCache();
closeEntityManager(em);

if (baseResult.size() != testResult.size()) {
fail("Criteria query using ON clause with a left join did not match JPQL results; "
+baseResult.size()+" were expected, while criteria query returned "+testResult.size());
}
}

/////UPDATE Criteria tests:
public void simpleMetamodelCriteriaUpdateTest()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import jakarta.persistence.PersistenceException;
import jakarta.persistence.TemporalType;
import jakarta.persistence.TypedQuery;

import org.eclipse.persistence.exceptions.QueryException;
import org.eclipse.persistence.expressions.Expression;
import org.eclipse.persistence.internal.databaseaccess.DatasourcePlatform;
Expand Down Expand Up @@ -296,43 +295,44 @@ public TypedQuery<X> setHint(String hintName, Object value) {
* if not a Java Persistence query language SELECT query
*/
@Override
public EJBQueryImpl setLockMode(LockModeType lockMode) {
return (EJBQueryImpl) super.setLockMode(lockMode);
@SuppressWarnings("unchecked")
public EJBQueryImpl<X> setLockMode(LockModeType lockMode) {
return (EJBQueryImpl<X>) super.setLockMode(lockMode);
}

// TODO-API-3.2

// Based on EntityManagerImpl#getQueryHints(Object,OperationType)
@Override
public CacheRetrieveMode getCacheRetrieveMode() {
throw new UnsupportedOperationException("Jakarta Persistence 3.2 API was not implemented yet");
return FindOptionUtils.getCacheRetrieveMode(entityManager.getAbstractSession(), getDatabaseQuery().getProperties());
}

// TODO-API-3.2
@Override
public TypedQuery<X> setCacheRetrieveMode(CacheRetrieveMode cacheRetrieveMode) {
throw new UnsupportedOperationException("Jakarta Persistence 3.2 API was not implemented yet");
FindOptionUtils.setCacheRetrieveMode(getDatabaseQuery().getProperties(), cacheRetrieveMode);
return this;
}

// TODO-API-3.2
@Override
public CacheStoreMode getCacheStoreMode() {
throw new UnsupportedOperationException("Jakarta Persistence 3.2 API was not implemented yet");
return FindOptionUtils.getCacheStoreMode(entityManager.getAbstractSession(), getDatabaseQuery().getProperties());
}

// TODO-API-3.2
@Override
public TypedQuery<X> setCacheStoreMode(CacheStoreMode cacheStoreMode) {
throw new UnsupportedOperationException("Jakarta Persistence 3.2 API was not implemented yet");
FindOptionUtils.setCacheStoreMode(getDatabaseQuery().getProperties(), cacheStoreMode);
return this;
}

// TODO-API-3.2
@Override
public Integer getTimeout() {
throw new UnsupportedOperationException("Jakarta Persistence 3.2 API was not implemented yet");
return FindOptionUtils.getTimeout(entityManager.getAbstractSession(), getDatabaseQuery().getProperties());
}

@Override
public TypedQuery<X> setTimeout(Integer timeout) {
throw new UnsupportedOperationException("Jakarta Persistence 3.2 API was not implemented yet");
FindOptionUtils.setTimeout(getDatabaseQuery().getProperties(), timeout);
return this;
}

/**
Expand Down
Loading
Loading