Wrong query using filter and entity inheritance

Hi all,
I’m having some trouble using @Filter annotation in combination with @Inheritance(strategy=InheritanceType.JOINED) in Hibernate 5.4.15.Final

The scenario is like this:

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@Table(name = "thing")
@FilterDef(name = "nameFilter", parameters = {@ParamDef(name = "nameParam", type = "string")})
@Filter(name = "nameFilter", condition = "(name = :nameParam)")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public abstract class Thing implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "hibernate_sequence")
    @SequenceGenerator(name = "hibernate_sequence")
    private Long id;

    @Column(name = "name")
    private String name;
}
@Entity
@Table(name = "place")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Place extends Thing implements Serializable {

	@Column(name = "parent_id")
	private Long parent_id;

 	@Column(name = "address")
    private String address;
}
@Entity
@Table(name = "reservation")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Reservation implements Serializable {

    private static final long serialVersionUID = 1L;

    @Column(name = "check_in_time")
    private ZonedDateTime checkInTime;

    @Column(name = "check_out_time")
    private ZonedDateTime checkOutTime;

    @ManyToOne(cascade=CascadeType.MERGE, fetch = FetchType.EAGER)
    @JoinColumn(name="reservation_for_id", referencedColumnName="parent_id")
    @Fetch(FetchMode.SELECT)
    private Place reservationFor;
}

When in my repository class I’ve defined this method

@Query("SELECT lr "
	 + "  FROM LodgingReservation lr "
	 + "    LEFT JOIN FETCH lr.reservationFor reservationFor "
	 + " WHERE currentTimestampWithoutTimezone() BETWEEN lr.checkInTime AND lr.checkOutTime ")
List<Reservation> findActiveReservations();

the resulting query is wrongly formatted and the filter is attached to the “child” table instead of the “parent” one causing an SQL Error:

select
	res0_.id as id1_14_0_,
	place1_.id as id1_14_1_,
	res0_.check_in_time as check_in1_7_0_,
	res0_.check_out_time as check_ou2_7_0_,
	res0_.reservation_status as reservat7_12_0_
	res0_.reservation_for_id as reservat5_7_0_,
	place1_1_.name as name8_14_1_,
	place1_.address as address1_11_1_,
	place1_.parent_id as parent_10_11_1_
from reservation res0_ 
  left outer join place place1_ on res0_.reservation_for_id = place1_.parent_id and (**place1_1_.name = ?**)
  left outer join thing place1_1_ on place1_.id = place1_1_.id
where res0_.reservation_status = 'IN_PROGRESS'

Anyone encountered the same problem?

Valerio

That’s probably a bug. You should create an issue in the issue tracker(https://hibernate.atlassian.net) with a test case(https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java) that reproduces the issue.

Issue created: HHH-14639

1 Like