# Hibernate 6 Wrong query generated with @DiscriminatorValue

**URL:** <https://discourse.hibernate.org/t/hibernate-6-wrong-query-generated-with-discriminatorvalue/6984>\
**Category:** Hibernate ORM\
**Created:** [December 6, 2022, 8:29am UTC](https://discourse.hibernate.org/t/hibernate-6-wrong-query-generated-with-discriminatorvalue/6984 "2022-12-06T08:29:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gaetan\_N](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/gaetan_n/32/2143_2.png) [@Gaetan\_N](https://discourse.hibernate.org/u/Gaetan_N)\
**Post date:** [December 6, 2022, 8:29am UTC](https://discourse.hibernate.org/t/hibernate-6-wrong-query-generated-with-discriminatorvalue/6984/1 "2022-12-06T08:29:07Z")

</div>

I created this repo to highlight the issue with spring boot 3 / Hibernate 6.1.5 : [GitHub - gaetannandelec-ibboost/hibernate6-discriminatorbug](https://github.com/gaetannandelec-ibboost/hibernate6-discriminatorbug)

I have the following classes

```auto
    @Entity
    @Table(name = "users")
    public class User extends BaseEntity implements Serializable {
    
    ..
    
    	@OneToMany(fetch = FetchType.EAGER, cascade = { CascadeType.MERGE, CascadeType.PERSIST }, mappedBy = "entity")
    	private Set<UserUdfValue> properties = new HashSet<>();
    
    ..
    
    }

    @Entity
    @DiscriminatorValue(UserUdfValue.UDFID)
    public class UserUdfValue extends ValueBase {
    
    	private static final long serialVersionUID = -898152830344556386L;
    
    	public final static String UDFID = "1";
    
    	@ManyToOne(fetch=FetchType.LAZY, cascade = {})
    	@JoinColumn(name = "entity_id", nullable = false)
    	private User entity;
    
    	public User getEntity() {
    		return entity;
    	}
    
    	public void setEntity(User entity) {
    		this.entity = entity;
    	}
    
    }

    @Entity(name = "udf_value")
    @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
    @DiscriminatorColumn(name = "target_type", discriminatorType = DiscriminatorType.INTEGER)
    @DiscriminatorOptions(force = false)
    public abstract class ValueBase implements Serializable {
    	private static final long serialVersionUID = 1L;
    
    	@Id
    	@GeneratedValue(strategy = GenerationType.AUTO)
    	private Long id;
    	
    ...
    
    }

```

Prior migrating to hibernate 6 ( hibernate 5.6 ), when querying a user by id , it was generating an sql query like i expect

```auto
    SELECT *
    FROM users user0_
    LEFT OUTER JOIN udf_value properties14_ ON user0_.id = properties14_.entity_id
        AND properties14_.target_type = 1
    WHERE user0_.id = ?

```

However with hibernate 6 the discriminator criteria is no longer on the join but in the where clause

```auto
    SELECT *
    FROM users u1_0
    LEFT JOIN udf_value p2_0 ON u1_0.id = p2_0.entity_id
    WHERE p2_0.target_type = 1
        AND u1_0.id = ?

```

Which obviously doesn’t give me the same result i expect

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [December 6, 2022, 3:50pm UTC](https://discourse.hibernate.org/t/hibernate-6-wrong-query-generated-with-discriminatorvalue/6984/2 "2022-12-06T15:50:43Z")

</div>

Yep, that’s a bug. Thanks for reporting it! Please create an issue in the issue tracker([https://hibernate.atlassian.net](https://hibernate.atlassian.net)) and in there refer to this discourse topic and the GitHub repository you created.

---

<div class="post-metadata">

**Author:** ![Gaetan\_N](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/gaetan_n/32/2143_2.png) [@Gaetan\_N](https://discourse.hibernate.org/u/Gaetan_N)\
**Post date:** [December 6, 2022, 5:03pm UTC](https://discourse.hibernate.org/t/hibernate-6-wrong-query-generated-with-discriminatorvalue/6984/3 "2022-12-06T17:03:08Z")

</div>

Thank you, i created a ticket for it [[HHH-15829] - Hibernate JIRA](https://hibernate.atlassian.net/browse/HHH-15829)

---

<div class="post-metadata">

**Author:** ![cmuppet](https://avatars.discourse-cdn.com/v4/letter/c/7c8e57/32.png) [@cmuppet](https://discourse.hibernate.org/u/cmuppet)\
**Post date:** [January 1, 2023, 1:43pm UTC](https://discourse.hibernate.org/t/hibernate-6-wrong-query-generated-with-discriminatorvalue/6984/4 "2023-01-01T13:43:10Z")

</div>

We are in the process of upgrading to version 6. Our datamodel is relying heavily on entities with discriminator colum and quries containing left joins are broken. Rewriting all queries is no option.

Is there any workaround for this problem, apart from waiting or stopping the upgrade?

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [January 4, 2023, 7:15pm UTC](https://discourse.hibernate.org/t/hibernate-6-wrong-query-generated-with-discriminatorvalue/6984/5 "2023-01-04T19:15:28Z")

</div>

No workaround. You’ll have to wait for the fix.
