We're updating the issue view to help you get more done. 

Query "select count(h) from Human h" fails if a subclass has a non-Id property named "id"

Description

Given the following classes:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 @Entity(name = "Genius") public static class Genius extends Human { private Long id; public Genius() { } public Genius(Long id) { this.id = id; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } } @Entity(name = "Human") @Inheritance(strategy = InheritanceType.SINGLE_TABLE) public static class Human { private Long realId; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "realId") public Long getRealId() { return realId; } public void setRealId(Long realId) { this.realId = realId; } }

Executing the query: select count( h ) from Human h results in the following SQL:

1 2 3 4 select count(idproperty0_.id) as col_0_0_ from Human idproperty0_

Note that:

  • idproperty0_.id is not the primary key column;

  • Human does not have a property named id;

  • count should be operating on the primary key, idproperty0_.realId.

The same select expression is generated using @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS).

When @Inheritance(strategy = InheritanceType.JOINED) is used, the generated SQL is:

1 2 3 4 select count(idproperty0_1_.id) as col_0_0_ from Human idproperty0_

In addition to using the wrong column, there is no table with alias idproperty0_1_.

Environment

None

Status

Assignee

Andrea Boriero

Reporter

Gail Badner

Fix versions

Labels

None

backPortable

Backport?

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

5.2.18
5.4.0.CR1
5.3.7
5.1.16

Priority

Major