Skip to content

Modifying query with multitenancy generates invalid sql #1975

@Klompara

Description

@Klompara

Updating entities containing @TenantId produces invalid update query
Example:

@Entity
public class MyEntity {

    @Id
    @JdbcTypeCode(SqlTypes.CHAR)
    private UUID id;

    @Column
    private String name;

    @TenantId
    private String tenantId;

}
public interface MyRepository extends JpaRepository<MyEntity, UUID> {

    @Modifying
    @Transactional
    @Query("UPDATE MyEntity SET name = :name WHERE id = :id")
    void updateNameById(String name, UUID id);

}
@Service
public class MyService {

    @Autowired
    private MyRepository myRepository;

    @PostConstruct
    public void updateEntity() {
        myRepository.updateNameById("Updated Name", UUID.randomUUID());
    }

}

The resulting SQL is printed as the following:
Hibernate: update MyEntity set name=? where id=? and me1_0.tenantId = ?

In the query we can see that the tenantId is automatically appended and contains the table alias me1_0
However this alias is not set and results in an error
Unrecognized name: me1_0 [at 1:47]

Issue does also occur when directly executing update query with EntityManager.
Valid SQL is produced when using H2 or Postgres

Reproducible code sample:

sample.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions