[hibernate-issues] [Hibernate-JIRA] Created: (HHH-5169) Columns from other entity with same table name as base class name of table-per-subclass hierarchy appears in SQL queries

Daniël van 't Ooster (JIRA) noreply at atlassian.com
Wed Apr 28 05:50:34 EDT 2010


Columns from other entity with same table name as base class name of table-per-subclass hierarchy appears in SQL queries
------------------------------------------------------------------------------------------------------------------------

                 Key: HHH-5169
                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-5169
             Project: Hibernate Core
          Issue Type: Bug
          Components: core
    Affects Versions: 3.5.1, 3.3.2
         Environment: Tested with 3.5.1-Final and 3.3.2.GA
            Reporter: Daniël van 't Ooster
            Priority: Minor


Sorry for the long title, couln't find anything better which covers the problem.. While mixing some legacy mapping with some new code, Hibernate is mixing some legacy columns and tables with the new ones. Please consider the following mapping.

{code}
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Bar {
	@Column(name = "BarId")
	@Id
	private long m_id;
	
	// irrelevant stuff omitted
}

@Entity
@Table(name = "Foo")
public class Foo extends Bar {

	// irrelevant stuff omitted
}
{code}

So far so good. Now I add some other mappings for the same domain, so they share some names (I have named it NewBar to avoid problems in HQL queries, but this it not relevant for this case).

{code}
@Entity(name = "NewBar")
@Table(name = "Bar")
public class NewBar {
	@Id
	@Column(name = "NewBarId")
	@GeneratedValue
	private long m_id;

	// irrelevant stuff omitted
}
{code}

The generated schema (for H2) is:
{code}
# Schema for NewBar
create table Bar (
	NewBarId bigint generated by default as identity,
	primary key (NewBarId)
)

# Schema for old Bar and Foo
create table Foo (
	BarId bigint not null,
	primary key (BarId)
)
{code}

Nothing wrong with the schema.

Now, when I want to count all old Bar and Foo objects, I execute
{code}
getSession()
  .createCriteria(Bar.class)
  .setProjection(Projections.rowCount())
  .uniqueResult();
{code}

Generated SQL:
{code}
    select
        count(*) as y0_ 
    from
        ( select
            BarId,
            NewBarId,
            0 as clazz_ 
        from
            Bar 
        union
        all select
            BarId,
            NewBarId,
            1 as clazz_ 
        from
            Foo 
    ) this_
{code}
So the columns of the Bar entity and the NewBar (which uses the Bar table) are mixed. In some way, Hibernate is expecting to have a table for the base class of a table-per-subclass hierarchy. Because this is not specified, it uses its entity name. That Table instance is shared over both the Bar and NewBar classes, which result in a from clause which mixes columns from both entities. In the query, nothing is done with the NewBar class, so its columns should not appear in the query.

Workaround is not hard to implement: add a @Table annotation with a unique table name to the base class of the table-per-subclass hierarchy (in this case Bar). This table will not appear in a schema export.


-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

       



More information about the hibernate-issues mailing list