Skip to content

Commit

Permalink
[Blazebit#1417] Fix lookup of constantified attributes in functional …
Browse files Browse the repository at this point in the history
…dependency analysis
  • Loading branch information
beikov committed Jan 16, 2022
1 parent afaf2ee commit 38ac606
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Not yet released

* Fix dev UI support for Quarkus 2
* Fix support for non-public `@PostLoad` methods in entity view annotation processor
* Fix lookup of constantified attributes in functional dependency analysis

### Backwards-incompatible changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public Boolean visit(PathExpression expr) {
baseNodeKey = new AbstractMap.SimpleEntry<>(baseNode, associationName);
if (attribute.getPersistentAttributeType() != Attribute.PersistentAttributeType.ONE_TO_ONE) {
boolean nonConstantParent = true;
Map<String, Boolean> constantifiedAttributes = constantifiedJoinNodeAttributeCollector.getConstantifiedJoinNodeAttributes().get(baseNode);
Map<String, Boolean> constantifiedAttributes = constantifiedJoinNodeAttributeCollector.getConstantifiedJoinNodeAttributes().get(baseNodeKey);
if (constantifiedAttributes != null) {
Map<String, Boolean> orderedAttributes = new HashMap<>();
addAttributes(baseNode.getEntityType(), null, "", "", (SingularAttribute<?, ?>) attribute, orderedAttributes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package com.blazebit.persistence.testsuite.entity;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;
import java.io.Serializable;
Expand All @@ -34,6 +37,7 @@ public class DocumentWithNullableName extends Ownable implements Serializable {
private static final long serialVersionUID = 1L;

private String name;
private Person friend;
private List<Person> people = new ArrayList<Person>();

public DocumentWithNullableName() {
Expand All @@ -50,6 +54,7 @@ public DocumentWithNullableName(String name) {
public DocumentWithNullableName(String name, Person owner) {
this(name);
this.setOwner(owner);
this.setFriend(owner);
}

public String getName() {
Expand All @@ -60,6 +65,16 @@ public void setName(String name) {
this.name = name;
}

@ManyToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "friend_id")
public Person getFriend() {
return friend;
}

public void setFriend(Person friend) {
this.friend = friend;
}

@OneToMany
@OrderColumn(name = "people_idx", nullable = false)
@JoinTable(name = "documentnullablename_people")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public void work(EntityManager em) {
doc2.setOwner(o2);
doc3.setOwner(o3);
doc4.setOwner(o4);
doc1.setFriend(o1);
doc2.setFriend(o2);
doc3.setFriend(o3);
doc4.setFriend(o4);

em.persist(o1);
em.persist(o2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
*/
public class OptimizedKeysetPaginationTest extends AbstractCoreTest {

private static Person o4;

@Override
public void setUpOnce() {
cleanDatabase();
Expand All @@ -64,14 +66,20 @@ public void work(EntityManager em) {
Person o1 = new Person("Karl1");
Person o2 = new Person("Karl2");
Person o3 = new Person("Karl3");
Person o4 = new Person("Karl4");
o4 = new Person("Karl4");

doc1.setOwner(o1);
doc2.setOwner(o2);
doc3.setOwner(o3);
doc4.setOwner(o4);
doc5.setOwner(o4);
doc6.setOwner(o4);
doc1.setFriend(o1);
doc2.setFriend(o2);
doc3.setFriend(o3);
doc4.setFriend(o4);
doc5.setFriend(o4);
doc6.setFriend(o4);

em.persist(o1);
em.persist(o2);
Expand Down Expand Up @@ -382,4 +390,28 @@ public List<String> buildList(List<String> list) {
assertEquals(1, result.size());
assertEquals("doc2 - Karl2", result.get(0));
}

@Test
public void optimizedKeysetPaginationWithConstantifiedSingleValuedPath() {
CriteriaBuilder<Tuple> crit = cbf.create(em, Tuple.class).from(DocumentWithNullableName.class, "d")
.select("d.name")
// Constantifying a many-to-one through a single valued id path leads to assuming the `d.friend.id` path is unique
.where("d.owner.id").eqLiteral(o4.getId());
crit.orderByDesc("d.friend.id")
// We need a nullable column in the order by clause to force the emulation rendering
.orderByAsc("d.name")
.orderByAsc("d.id");

PaginatedCriteriaBuilder<Tuple> pcb = crit.page(null, 0, 1);
PagedList<Tuple> result = pcb.getResultList();

assertEquals(1, result.getSize());
assertEquals("doc4", result.get(0).get(0));

result = crit.page(result.getKeysetPage(), 1, 2).getResultList();

assertEquals(2, result.getSize());
assertEquals("doc5", result.get(0).get(0));
assertEquals("doc6", result.get(1).get(0));
}
}

0 comments on commit 38ac606

Please sign in to comment.