Skip to content

Commit

Permalink
Merge pull request #29669 from KyleAure/jakarta-data-todos-3
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleAure authored Sep 18, 2024
2 parents bd4bc3b + 1ed8da7 commit 53c9f8d
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ protected void collectEntityInfo(Set<Class<?>> entityTypes) throws Exception {
Class<?> idType = null;
String versionAttrName = null;

Exception collectionException = null;
Class<?> jpaEntityClass = entityType.getJavaType();
Class<?> userEntityClass = recordClass == null ? jpaEntityClass : recordClass;

try {
for (Attribute<?, ?> attr : entityType.getAttributes()) {
Expand Down Expand Up @@ -239,10 +240,6 @@ protected void collectEntityInfo(Set<Class<?>> entityTypes) throws Exception {
+ " which results in an overloaded path expression " + relationAttributeNameUndelimited
+ " the relational attribute will be ignored."); //TODO NLS swap to warning

// TODO - remove this experimental behavior - not part of Jakarta Data Specification
relationAttributeName = relationAttributeName.toLowerCase();
attributeNames.putIfAbsent(relationAttributeName, fullAttributeName);

attributeAccessors.put(fullAttributeName, relAccessors);

attributeTypes.put(fullAttributeName, relAttr.getJavaType());
Expand Down Expand Up @@ -282,40 +279,28 @@ protected void collectEntityInfo(Set<Class<?>> entityTypes) throws Exception {
}
}
}
} catch (MappingException e) {
collectionException = e;
} catch (Exception e) {
if (trace & tc.isDebugEnabled())
Tr.debug(tc, "Unexpected exception caught while collecting entity information. The entity information will complete exceptionally", e);
collectionException = e;
// TODO compute the userEntityClass earlier and completeExceptionally here.
// Then move the EntityInfo creation and successful completion to before the catch.
// That will also cover the possibility of EntityInfo constructor failing,
// which can happen when it doesn't validate.
}

Class<?> jpaEntityClass = entityType.getJavaType();
Class<?> userEntityClass = recordClass == null ? jpaEntityClass : recordClass;
EntityInfo entityInfo = new EntityInfo( //
entityType.getName(), //
jpaEntityClass, //
recordClass, //
attributeAccessors, //
attributeNames, //
attributeNamesForUpdate, //
attributeTypes, //
collectionElementTypes, //
relationAttributeNames, //
idType, //
idClassAttributeAccessors, //
versionAttrName, //
this);

EntityInfo entityInfo = new EntityInfo( //
entityType.getName(), //
jpaEntityClass, //
recordClass, //
attributeAccessors, //
attributeNames, //
attributeNamesForUpdate, //
attributeTypes, //
collectionElementTypes, //
relationAttributeNames, //
idType, //
idClassAttributeAccessors, //
versionAttrName, //
this);

if (collectionException == null)
entityInfoMap.computeIfAbsent(userEntityClass, EntityInfo::newFuture).complete(entityInfo);
else
entityInfoMap.computeIfAbsent(userEntityClass, EntityInfo::newFuture).completeExceptionally(collectionException);
} catch (MappingException e) { // Ignored FFDC
entityInfoMap.computeIfAbsent(userEntityClass, EntityInfo::newFuture).completeExceptionally(e);
} catch (Exception e) { // Produce FFDC
entityInfoMap.computeIfAbsent(userEntityClass, EntityInfo::newFuture).completeExceptionally(e);
}
}
} finally {
if (em != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,29 @@
public interface Businesses extends BasicRepository<Business, Integer> {

// embeddable 1 level deep
List<Business> findByLatitudeBetweenOrderByLongitudeDesc(float min, float max);
List<Business> findByLocationLatitudeBetweenOrderByLocationLongitudeDesc(float min, float max);

// embeddable 3 levels deep where @Column resolves name conflict
Business[] findByLocation_Address_Street_NameIgnoreCaseEndsWithOrderByLocation_Address_Street_DirectionIgnoreCaseAscNameAsc(String streetName);

List<Business> findByLocationLongitudeAbsoluteValueBetween(float min, float max);

// embeddable as result type
@OrderBy("street")
@OrderBy("houseNum")
Stream<Location> findByZip(int zipCode);
@OrderBy("location.address.street")
@OrderBy("location.address.houseNum")
Stream<Location> findByLocationAddressZip(int zipCode);

// embeddable 2 levels deep
@OrderBy(value = "city", descending = true)
@OrderBy(value = "location.address.city", descending = true)
@OrderBy("location.address.zip")
@OrderBy("houseNum")
@OrderBy("location.address.houseNum")
@OrderBy("id")
CursoredPage<Business> findByZipIn(Iterable<Integer> zipCodes, PageRequest pagination);
CursoredPage<Business> findByLocationAddressZipIn(Iterable<Integer> zipCodes, PageRequest pagination);

// embeddable 3 levels deep as result type
@OrderBy("street")
@OrderBy("houseNum")
Stream<Street> findByZipNotAndCity(int excludeZipCode, String city);
@OrderBy("location.address.street")
@OrderBy("location.address.houseNum")
Stream<Street> findByLocationAddressZipNotAndLocationAddressCity(int excludeZipCode, String city);

@OrderBy("id")
Business findFirstByName(String name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public interface Customers extends DataRepository<Customer, Integer> {
@OrderBy("email")
Stream<Customer> findByPhoneIn(List<Long> phoneNumbers);

@OrderBy("phone")
Stream<CreditCard> findCardsByEmailEndsWith(String ending);
@OrderBy("debtor.phone")
Stream<CreditCard> findCardsByDebtorEmailEndsWith(String ending);

Set<CreditCard> findCardsByCustomerId(int customerId);
Set<CreditCard> findCardsByDebtorCustomerId(int customerId);

@OrderBy("email")
@Query("SELECT c.email FROM Customer c JOIN c.cards cc WHERE (cc.issuer=?1)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,7 @@ public void testEmbeddable() {
added.stream().map(a -> a.id).collect(Collectors.toSet()));

assertArrayEquals(new ShippingAddress[] { a4, a2 },
shippingAddresses.findByStreetNameOrderByHouseNumber("4th St SE"),
shippingAddresses.findByStreetAddress_streetNameOrderByStreetAddress_houseNumber("4th St SE"),
Comparator.<ShippingAddress, Long> comparing(o -> o.id)
.thenComparing(Comparator.<ShippingAddress, String> comparing(o -> o.city))
.thenComparing(Comparator.<ShippingAddress, String> comparing(o -> o.state))
Expand All @@ -968,7 +968,7 @@ Comparator.<ShippingAddress, Long> comparing(o -> o.id)
.thenComparing(Comparator.<ShippingAddress, Integer> comparing(o -> o.zipCode)));

assertIterableEquals(List.of("200 1st Ave SW", "151 4th St SE", "201 4th St SE"),
Stream.of(shippingAddresses.findByHouseNumberBetweenOrderByStreetNameAscHouseNumber(150, 250))
Stream.of(shippingAddresses.findByStreetAddress_houseNumberBetweenOrderByStreetAddress_streetNameAscStreetAddress_houseNumber(150, 250))
.map(a -> a.houseNumber + " " + a.streetName)
.collect(Collectors.toList()));

Expand All @@ -987,7 +987,7 @@ Comparator.<ShippingAddress, Long> comparing(o -> o.id)
// assertEquals(a1.streetAddress.streetName, a.streetAddress.streetName);
// assertEquals(a1.streetAddress.recipientInfo, a.streetAddress.recipientInfo);

// assertEquals(3L, shippingAddresses.countByRecipientInfoEmpty());
// assertEquals(3L, shippingAddresses.countByStreetAddressRecipientInfoEmpty());

// [EclipseLink-4002] Internal Exception: java.sql.SQLIntegrityConstraintViolationException:
// DELETE on table 'SHIPPINGADDRESS' caused a violation of foreign key constraint 'SHPPNGSHPPNGDDRSSD' for key (1001)
Expand Down Expand Up @@ -1084,7 +1084,7 @@ public void testEmbeddableCollection() {
@Test
public void testEmbeddableDepth1() {
assertIterableEquals(List.of("Olmsted Medical", "Mayo Clinic", "Home Federal Savings Bank", "Custom Alarm"),
businesses.findByLatitudeBetweenOrderByLongitudeDesc(44.0f, 44.03f)
businesses.findByLocationLatitudeBetweenOrderByLocationLongitudeDesc(44.0f, 44.03f)
.stream()
.map(b -> b.name)
.collect(Collectors.toList()));
Expand All @@ -1098,15 +1098,15 @@ public void testEmbeddableDepth2() {
CursoredPage<Business> page;
List<Integer> zipCodes = List.of(55906, 55902, 55901, 55976, 55905);

page = businesses.findByZipIn(zipCodes, PageRequest.ofSize(4).withoutTotal());
page = businesses.findByLocationAddressZipIn(zipCodes, PageRequest.ofSize(4).withoutTotal());

assertIterableEquals(List.of(345, 1421, 1016, 1600),
page
.stream()
.map(b -> b.location.address.houseNum)
.collect(Collectors.toList()));

page = businesses.findByZipIn(zipCodes, page.nextPageRequest());
page = businesses.findByLocationAddressZipIn(zipCodes, page.nextPageRequest());

assertIterableEquals(List.of(2800, 2960, 3100, 3428),
page
Expand All @@ -1117,7 +1117,7 @@ public void testEmbeddableDepth2() {
assertEquals(2L, page.pageRequest().page());
assertEquals(4, page.pageRequest().size());

page = businesses.findByZipIn(zipCodes, page.nextPageRequest());
page = businesses.findByLocationAddressZipIn(zipCodes, page.nextPageRequest());

assertIterableEquals(List.of(5201, 1661, 3706, 200),
page
Expand All @@ -1127,7 +1127,7 @@ public void testEmbeddableDepth2() {

assertEquals(3, page.pageRequest().page());

page = businesses.findByZipIn(zipCodes, page.nextPageRequest());
page = businesses.findByLocationAddressZipIn(zipCodes, page.nextPageRequest());

assertIterableEquals(List.of(1402, 3008),
page
Expand All @@ -1139,7 +1139,7 @@ public void testEmbeddableDepth2() {
assertEquals(4, page.pageRequest().page());
assertEquals(false, page.hasNext());

page = businesses.findByZipIn(zipCodes, page.previousPageRequest());
page = businesses.findByLocationAddressZipIn(zipCodes, page.previousPageRequest());

assertIterableEquals(List.of(5201, 1661, 3706, 200),
page
Expand Down Expand Up @@ -1257,7 +1257,7 @@ public void testEmbeddableTypeAsResult() {
"NW Lakeridge Pl",
"NW Members Parkway",
"W Highway 14"),
businesses.findByZip(55901)
businesses.findByLocationAddressZip(55901)
.map(loc -> loc.address.street.direction + " " + loc.address.street.name)
.collect(Collectors.toList()));
}
Expand All @@ -1273,7 +1273,7 @@ public void testEmbeddableTypeAsResultDepth3() {
"SW 1st St",
"SW Enterprise Dr",
"SW Greenview Dr"),
businesses.findByZipNotAndCity(55901, "Rochester")
businesses.findByLocationAddressZipNotAndLocationAddressCity(55901, "Rochester")
.map(street -> street.direction + " " + street.name)
.collect(Collectors.toList()));
}
Expand Down Expand Up @@ -2282,7 +2282,7 @@ public void testInheritance() {
assertEquals("37th St NW", a.streetAddress.streetName);
assertEquals(55901, a.zipCode);

WorkAddress[] secondFloorOfficesOn37th = shippingAddresses.findByStreetNameAndFloorNumber("37th St NW", 2);
WorkAddress[] secondFloorOfficesOn37th = shippingAddresses.findByStreetAddress_streetNameAndFloorNumber("37th St NW", 2);

assertArrayEquals(new WorkAddress[] { work }, secondFloorOfficesOn37th,
Comparator.<WorkAddress, Long> comparing(o -> o.id)
Expand All @@ -2294,7 +2294,7 @@ Comparator.<WorkAddress, Long> comparing(o -> o.id)
.thenComparing(Comparator.<WorkAddress, Integer> comparing(o -> o.streetAddress.houseNumber))
.thenComparing(Comparator.<WorkAddress, Integer> comparing(o -> o.zipCode)));

ShippingAddress[] found = shippingAddresses.findByStreetNameOrderByHouseNumber("37th St NW");
ShippingAddress[] found = shippingAddresses.findByStreetAddress_streetNameOrderByStreetAddress_houseNumber("37th St NW");

assertArrayEquals(new ShippingAddress[] { work }, found,
Comparator.<ShippingAddress, Long> comparing(o -> o.id)
Expand All @@ -2306,7 +2306,7 @@ Comparator.<ShippingAddress, Long> comparing(o -> o.id)
.thenComparing(Comparator.<ShippingAddress, Integer> comparing(o -> o.streetAddress.houseNumber))
.thenComparing(Comparator.<ShippingAddress, Integer> comparing(o -> o.zipCode)));

StreetAddress[] streetAddresses = shippingAddresses.findByHouseNumberBetweenOrderByStreetNameAscHouseNumber(1000, 3000);
StreetAddress[] streetAddresses = shippingAddresses.findByStreetAddress_houseNumberBetweenOrderByStreetAddress_streetNameAscStreetAddress_houseNumber(1000, 3000);

assertArrayEquals(new StreetAddress[] { work.streetAddress, home.streetAddress }, streetAddresses,
Comparator.<StreetAddress, Integer> comparing(o -> o.houseNumber)
Expand Down Expand Up @@ -2820,7 +2820,7 @@ public void testOneToManyCustomJoinQuery() {
// TODO Could this be achieved with @Select?
public void testOneToManyReturnsCombinedCollectionFromMany() {

List<Long> cardNumbers = customers.findCardsByEmailEndsWith("[email protected]")
List<Long> cardNumbers = customers.findCardsByDebtorEmailEndsWith("[email protected]")
.map(card -> card.number)
.collect(Collectors.toList());

Expand All @@ -2837,7 +2837,7 @@ public void testOneToManyReturnsCombinedCollectionFromMany() {
*/
@Test
public void testOneToManyReturnsOneSetOfMany() {
Set<CreditCard> cards = customers.findCardsByCustomerId(9210005);
Set<CreditCard> cards = customers.findCardsByDebtorCustomerId(9210005);

assertEquals(cards.toString(), 2, cards.size());

Expand Down Expand Up @@ -2900,7 +2900,7 @@ public void testOneToOne() {
assertEquals("Oscar TestOneToOne", d.fullName);

// Query by an attribute of the entity to which OneToOne maps:
d = drivers.findByLicenseNum("T121-200-200-200");
d = drivers.findByLicense_licenseNum("T121-200-200-200");
assertEquals("Oliver TestOneToOne", d.fullName);

// Query by and order by attributes of the entity to which OneToOne maps:
Expand All @@ -2918,7 +2918,7 @@ public void testOneToOne() {
assertIterableEquals(List.of("Minnesota T121-100-100-100", "Minnesota T121-300-300-300",
"Wisconsin T121-500-500-500", "Wisconsin T121-200-200-200",
"Iowa T121-400-400-400"),
drivers.findByFullNameEndsWith(" TestOneToOne")
drivers.findByDriver_fullNameEndsWith(" TestOneToOne")
.map(license -> license.stateName + " " + license.licenseNum)
.collect(Collectors.toList()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public interface Drivers extends BasicRepository<Driver, Integer> {

int deleteByFullNameEndsWith(String ending);

@OrderBy("birthday")
Stream<DriversLicense> findByFullNameEndsWith(String ending);
@OrderBy("driver.birthday")
Stream<DriversLicense> findByDriver_fullNameEndsWith(String ending);

Driver findByLicense(DriversLicense license);

Expand All @@ -39,7 +39,7 @@ public interface Drivers extends BasicRepository<Driver, Integer> {
@OrderBy("licenseExpiresOn")
Stream<Driver> findByLicenseNotNull();

Driver findByLicenseNum(String licenseNumber);
Driver findByLicense_licenseNum(String licenseNumber);

Stream<Driver> findByLicenseStateNameOrderByLicenseExpiresOnDesc(String state);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
*/
@Repository
public interface ShippingAddresses {
long countByRecipientInfoEmpty();
long countByStreetAddressRecipientInfoEmpty();

StreetAddress[] findByHouseNumberBetweenOrderByStreetNameAscHouseNumber(int minHouseNumber, int maxHouseNumber);
StreetAddress[] findByStreetAddress_houseNumberBetweenOrderByStreetAddress_streetNameAscStreetAddress_houseNumber(int minHouseNumber, int maxHouseNumber);

List<ShippingAddress> findByRecipientInfoNotEmpty();
List<ShippingAddress> findByStreetAddress_recipientInfoNotEmpty();

WorkAddress[] findByStreetNameAndFloorNumber(String streetName, int floorNumber);
WorkAddress[] findByStreetAddress_streetNameAndFloorNumber(String streetName, int floorNumber);

ShippingAddress[] findByStreetNameOrderByHouseNumber(String streetName);
ShippingAddress[] findByStreetAddress_streetNameOrderByStreetAddress_houseNumber(String streetName);

@Query("SELECT o FROM WorkAddress o WHERE o.office=?1")
WorkAddress forOffice(String officeNum);
Expand Down

0 comments on commit 53c9f8d

Please sign in to comment.