Skip to content

Commit

Permalink
[#1859] Fix collection insert bug triggered by Hibernate ORM 6.4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
beikov committed Jan 25, 2024
1 parent 47915f4 commit 191e130
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ None yet

### Bug fixes

None yet
* Fix bug in collection insert code triggered by Hibernate ORM 6.4.2

### Backwards-incompatible changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ private <R> QuerySpecification getQuerySpecification(Query baseQuery, Query exam
for (Map.Entry<String, String> entry : joinTable.getTargetColumnMappings().entrySet()) {
columnExpressionRemappings.put(targetAlias + "." + entry.getValue(), entry.getKey());
columnExpressionRemappings.put(targetAlias + "." + entry.getKey(), entry.getKey());
columnExpressionRemappings.put(collectionAlias + "." + entry.getKey(), entry.getKey());
}

int cutoffColumns = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public static void expandBindings(Map<String, Integer> bindingMap, Map<String, S

joinManager.implicitJoin(baseExpression, true, true, true, null, ClauseType.SELECT, new HashSet<String>(), false, false, false, false);

if (elementType != baseExpression.getPathReference().getType().getJavaType()) {
if (!elementType.isAssignableFrom(baseExpression.getPathReference().getType().getJavaType())) {
throw new IllegalStateException("An association should be bound to its association type and not its identifier type");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.MapKeyColumn;
Expand Down Expand Up @@ -47,6 +48,9 @@ public class Root {
@OneToMany
@JoinTable(name = "list_one_to_many_set")
private Set<Parent> nodes = new HashSet<>();
@ManyToMany
@JoinTable(name = "set_one_to_many_poly", joinColumns = @JoinColumn(name = "root_id"), inverseJoinColumns = @JoinColumn(name = "poly_id"))
private Set<Sub1> nodesPoly = new HashSet<>();

@OneToMany
@JoinTable(name = "list_one_to_many")
Expand Down Expand Up @@ -120,6 +124,14 @@ public void setNodes(Set<Parent> nodes) {
this.nodes = nodes;
}

public Set<Sub1> getNodesPoly() {
return nodesPoly;
}

public void setNodesPoly(Set<Sub1> nodesPoly) {
this.nodesPoly = nodesPoly;
}

public List<IndexedNode> getIndexedNodes() {
return indexedNodes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -685,4 +685,30 @@ public void work(EntityManager em) {
}
});
}

// test for #1859
@Test
public void insertPoly() {
transactional(new TxVoidWork() {
@Override
public void work(EntityManager em) {
InsertCriteriaBuilder<Root> criteria = cbf.insertCollection(em, Root.class, "nodesPoly");
criteria.fromIdentifiableValues(Sub1.class, "val", 1);
criteria.bind("id").select("1");
criteria.bind("nodesPoly").select("val");
criteria.setParameter("val", Collections.singletonList(em.getReference(Sub1.class, p1Id)));

assertEquals("INSERT INTO Root.nodesPoly(id, nodesPoly.id)\n"
+ "SELECT 1, val.id"
+ " FROM Sub1(1 ID VALUES) val", criteria.getQueryString());
int updated = criteria.executeUpdate();
Root r = getRoot(em);

assertEquals(1, updated);
assertEquals(1, r.getNodesPoly().size());

assertEquals(p1Id, r.getNodesPoly().iterator().next().getId());
}
});
}
}
2 changes: 1 addition & 1 deletion parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
<version.hibernate-5.6>5.6.10.Final</version.hibernate-5.6>
<version.hibernate-6.2>6.2.13.Final</version.hibernate-6.2>
<version.hibernate-6.3>6.3.1.Final</version.hibernate-6.3>
<version.hibernate-6.4>6.4.1.Final</version.hibernate-6.4>
<version.hibernate-6.4>6.4.2.Final</version.hibernate-6.4>

<version.datanucleus.base-4>4.1.17</version.datanucleus.base-4>
<version.datanucleus.rdbms-4>4.1.19</version.datanucleus.rdbms-4>
Expand Down

0 comments on commit 191e130

Please sign in to comment.