Skip to content

HHH-19579 Criteria update join - Column 'code' in SET is ambiguous #10559

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

Open
wants to merge 2 commits into
base: 7.0
Choose a base branch
from
Open
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 @@ -399,4 +399,13 @@ protected void renderStringContainsExactlyPredicate(Expression haystack, Express
needle.accept( this );
appendSql( ",'~','~~'),'?','~?'),'%','~%'),'%') escape '~'" );
}

@Override
protected void appendAssignmentColumn(ColumnReference column) {
column.appendColumnForWrite(
this,
getAffectedTableNames().size() > 1 && !(getStatement() instanceof InsertSelectStatement)
? determineColumnReferenceQualifier( column )
: null );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -390,4 +390,13 @@ protected void renderStringContainsExactlyPredicate(Expression haystack, Express
needle.accept( this );
appendSql( ",'~','~~'),'?','~?'),'%','~%'),'%') escape '~'" );
}

@Override
protected void appendAssignmentColumn(ColumnReference column) {
column.appendColumnForWrite(
this,
getAffectedTableNames().size() > 1 && !(getStatement() instanceof InsertSelectStatement)
? determineColumnReferenceQualifier( column )
: null );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -411,4 +411,13 @@ protected void renderStringContainsExactlyPredicate(Expression haystack, Express
needle.accept( this );
appendSql( ",'~','~~'),'?','~?'),'%','~%'),'%') escape '~'" );
}

@Override
protected void appendAssignmentColumn(ColumnReference column) {
column.appendColumnForWrite(
this,
getAffectedTableNames().size() > 1 && !(getStatement() instanceof InsertSelectStatement)
? determineColumnReferenceQualifier( column )
: null );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -437,4 +437,13 @@ protected void renderStringContainsExactlyPredicate(Expression haystack, Express
needle.accept( this );
appendSql( ",'~','~~'),'?','~?'),'%','~%'),'%') escape '~'" );
}

@Override
protected void appendAssignmentColumn(ColumnReference column) {
column.appendColumnForWrite(
this,
getAffectedTableNames().size() > 1 && !(getStatement() instanceof InsertSelectStatement)
? determineColumnReferenceQualifier( column )
: null );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@ protected void visitSetAssignment(Assignment assignment) {
final List<ColumnReference> columnReferences = assignable.getColumnReferences();
final Expression assignedValue = assignment.getAssignedValue();
if ( columnReferences.size() == 1 ) {
columnReferences.get( 0 ).appendColumnForWrite( this, null );
appendAssignmentColumn( columnReferences.get( 0 ) );
appendSql( '=' );
final SqlTuple sqlTuple = getSqlTuple( assignedValue );
if ( sqlTuple != null ) {
Expand All @@ -1175,7 +1175,7 @@ else if ( assignedValue instanceof SelectStatement ) {
char separator = OPEN_PARENTHESIS;
for ( ColumnReference columnReference : columnReferences ) {
appendSql( separator );
columnReference.appendColumnForWrite( this, null );
appendAssignmentColumn( columnReference );
separator = COMMA_SEPARATOR_CHAR;
}
appendSql( ")=" );
Expand All @@ -1184,7 +1184,7 @@ else if ( assignedValue instanceof SelectStatement ) {
else {
assert assignedValue instanceof SqlTupleContainer;
final List<? extends Expression> expressions = ( (SqlTupleContainer) assignedValue ).getSqlTuple().getExpressions();
columnReferences.get( 0 ).appendColumnForWrite( this, null );
appendAssignmentColumn( columnReferences.get( 0 ) );
appendSql( '=' );
expressions.get( 0 ).accept( this );
for ( int i = 1; i < columnReferences.size(); i++ ) {
Expand All @@ -1196,6 +1196,10 @@ else if ( assignedValue instanceof SelectStatement ) {
}
}

protected void appendAssignmentColumn(ColumnReference column) {
column.appendColumnForWrite( this, null );
}

protected void visitSetAssignmentEmulateJoin(Assignment assignment, UpdateStatement statement) {
final Assignable assignable = assignment.getAssignable();
if ( assignable instanceof SqmPathInterpretation<?> sqmPathInterpretation ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.orm.test.jpa;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaDelete;
import jakarta.persistence.criteria.CriteriaUpdate;
import jakarta.persistence.criteria.Join;
import jakarta.persistence.criteria.JoinType;
import jakarta.persistence.criteria.Root;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.Jpa;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

@Jpa(
annotatedClasses = {
CriteriaUpdateAndDeleteWithJoinTest.Parent.class,
CriteriaUpdateAndDeleteWithJoinTest.Child.class
}
)
@JiraKey( "HHH-19579" )
public class CriteriaUpdateAndDeleteWithJoinTest {
private static final String CHILD_CODE = "123";

@BeforeEach
public void setup(EntityManagerFactoryScope scope) {
scope.inTransaction(
entityManager -> {
Child child = new Child( 1L, CHILD_CODE );
Parent parent = new Parent( 2L, "456", child );
entityManager.persist( parent );
}
);
}

@AfterEach
public void teardown(EntityManagerFactoryScope scope) {
scope.releaseEntityManagerFactory();
}

@Test
public void testUpdate(EntityManagerFactoryScope scope) {
scope.inTransaction(
entityManager -> {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaUpdate<Parent> update = cb.createCriteriaUpdate(Parent.class);

Root<Parent> root = update.from(Parent.class);
Join<Parent,Child> joinColor = root.join("child", JoinType.INNER);

update.set(root.get("code"), "l1s2");
update.where(cb.equal(joinColor.get("code"), cb.parameter(String.class, "code")));

int count = entityManager.createQuery(update).setParameter("code", CHILD_CODE).executeUpdate();
assertThat(count).isEqualTo(1);
}
);
}

@Test
public void testDelete(EntityManagerFactoryScope scope) {
scope.inTransaction(
entityManager -> {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaDelete<Parent> delete = cb.createCriteriaDelete(Parent.class);

Root<Parent> root = delete.from(Parent.class);
Join<Parent,Child> joinColor = root.join("child", JoinType.INNER);

delete.where(cb.equal(joinColor.get("code"), cb.parameter(String.class, "code")));

int count = entityManager.createQuery(delete).setParameter("code", CHILD_CODE).executeUpdate();
assertThat(count).isEqualTo(1);
}
);
}

@Entity(name = "Parent")
public static class Parent {

@Id
private Long id;

@Column(name = "code")
private String code;

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinColumn(name = "color_id")
private Child child;

public Parent() {
}

public Parent(Long id, String code, Child child) {
this.id = id;
this.code = code;
this.child = child;
}

public Long getId() {
return id;
}

public String getCode() {
return code;
}

public Child getChild() {
return child;
}
}

@Entity(name = "Child")
public static class Child {

@Id
private Long id;

@Column(name = "code")
private String code;

public Child() {
}

public Child(Long id, String code) {
this.id = id;
this.code = code;
}

public Long getId() {
return id;
}

public String getCode() {
return code;
}
}
}
Loading