[hibernate-issues] [Hibernate-JIRA] Created: (HHH-7243) Wrong SQL statement generated due to a name collision of column aliases

Gerald Brose (JIRA) noreply at atlassian.com
Thu Apr 12 03:15:48 EDT 2012


Wrong SQL statement generated due to a name collision of column aliases
-----------------------------------------------------------------------

                 Key: HHH-7243
                 URL: https://hibernate.onjira.com/browse/HHH-7243
             Project: Hibernate ORM
          Issue Type: Bug
          Components: core
    Affects Versions: 3.6.10, 3.6.9
         Environment: Hibernate 3.6.9.Final, MS SQL Server 2008 R2, (Spring, JPA2)
            Reporter: Gerald Brose
            Priority: Critical


Under certain conditions, Hibernate generates a wrong SQL query due to a name collision of the generated column aliases used when accessing collections. 

This is done in the class AbstractCollectionPersister, which creates the same column alias (in our case the alias is "SUCHMASKE8_") for an index column (original column name: "SUCHMASKE_ORDER") and a foreign key column ("SUCHMASKE") in a OneToMany association. 

As a consequence, the resulting SQL statement selects from only one of the required columns. (In our case, this results in the integer value of a FK being used as a collection index, which in turn leads to a huge collection all initialized with null values).

Some information on the mapping:

from entity class SUCHMASKE:

    @JoinColumn(name="SUCHMASKE")
    @OneToMany
    @OrderColumn(name="SUCHMASKE_ORDER")
    public List<Suchkriterium> getSuchkriterien() {
        return suchkriterien;
    }

from entity class Suchkriterium:

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "SUCHMASKE")
    @MetaModelName("Suchmaske")
    public Suchmaske getSuchmaske() {
        return suchmaske;
    }

Both entity classes share the same ancestor class in the inheritance hierarchy, strategy is joined inheritance. There is no join table. The @JoinColumn is used work around the well-known Hibernate limitation that OrderColumns are not updated if specified on the non-owning side.


Analysis:

This alias name collision occurs in the case where both the unique integers used to create the aliases are the same (here: "8"). In one case this integer is coming from the root table in an inheritance hierarchy, in the other case from the column itself). Additionally, the two original column names share the same prefix ("SUCHMASKE"). Both these conditions are IMO completely legal. The integer value collision is somewhat random (so it took us days to identify the problem...). The prefix match is a even a regular naming scheme for order columns that correspond to an association.

The actual problem is that Hibernate uses slightly different ways to create these alias names for key columns and index columns (cf. AbstractCollectionPersister, lines 299 and 387, respectively).

As a workaround, we have to use an additional column naming convention for order columns so that common prefixes are avoided. This means we have to modify our existing schema and application, which is a nuisance. A bug fix in Hibernate would obviously be preferable.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list