@ManyToOne target using Single Table inheritance type

Description

It looks like the wrong SQL is generated when you use the @ManyToOne which target entity is using Single Table inheritance type, see the example below:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "D_TYPE")
@Table(name = "DICTIONARIES")
public abstract class DictionaryItem implements Serializable {

private String description;
private Long id;

@Basic
public String getDescription() {
return description;
}

@Id
public Long getId() {
return id;
}
...
}

@Entity
@DiscriminatorValue("MaterialType")
public class MaterialType extends DictionaryItem {

// Empty by design ...
}

@Entity
@javax.persistence.Table(name = "TABLES")
public class Table implements Serializable {

private Long id;
private MaterialType legsMaterial;

@Id
public Long getId() {
return id;
}

@ManyToOne
@JoinColumn(name = "LEG_MATERIAL")
public MaterialType getLegsMaterial() {
return legsMaterial;
}
...
}

@Repository("persistence.TableDAO")
public class DefaultTableDAO implements TableDAO {

@PersistenceContext
private EntityManager entityManager;

public Table get(Long id) {
return entityManager.find(Table.class, id);
}

...
}

DB tables:

CREATE TABLE DICTIONARIES (
ID NUMERIC NOT NULL,
D_TYPE VARCHAR(32),
DESCRIPTION VARCHAR(64),
CONSTRAINT DICTIONARIES_PK PRIMARY KEY (ID, D_TYPE)
);

INSERT INTO DICTIONARIES(ID, D_TYPE, DESCRIPTION) VALUES (1, 'MaterialType', 'Wood');
INSERT INTO DICTIONARIES(ID, D_TYPE, DESCRIPTION) VALUES (2, 'MaterialType', 'Glass');
INSERT INTO DICTIONARIES(ID, D_TYPE, DESCRIPTION) VALUES (1, 'SomeOtherType', 'SOT1');
INSERT INTO DICTIONARIES(ID, D_TYPE, DESCRIPTION) VALUES (2, 'SomeOtherType', 'SOT2');

CREATE TABLE TABLES (
ID NUMERIC NOT NULL,
LEG_MATERIAL NUMERIC,
CONSTRAINT TABLES_PK PRIMARY KEY (ID)
);

INSERT INTO TABLES(ID, LEG_MATERIAL) VALUES (1, 1);

Now let's try to fetch the table having id 1 with following code:

Table table = tableDAO.get(1L);

this will cause hibernate to execute the SQL query:

select table0_.id as id0_1_, table0_.LEG_MATERIAL as LEG2_0_1_, materialty1_.id as id1_0_, materialty1_.description as descript3_1_0_ from TABLES table0_ left outer join DICTIONARIES materialty1_ on table0_.LEG_MATERIAL=materialty1_.id where table0_.id=?

with parameter 1 (table id)

which leads to the following result set:

-------------------------------------------+

id0_1_

LEG2_0_1_

id1_0_

descript3_1_0_

-------------------------------------------+

1

1

1

Wood

1

1

1

SOT1

-------------------------------------------+

as you see - there is missing discriminator part of where clause for the material type

Environment

hibernate annotations 3.4.0 GA, hibernate entity manager 3.4.0 GA, spring framework 3.0.4, database: Mysql (tested on Oracle as well)

Assignee

Unassigned

Reporter

Michal Jastak

Labels

None

Feedback Requested

None

Feedback Requested By

None

backPortable

None

Suitable for new contributors

None

Pull Request

None

backportDecision

None

backportReEvaluate

None

Affects versions

Priority

Major
Configure