Hello, I would like to use the JPA 2.0 Type expression in a query inside a spring data JPA repository but it produces an error.
Below a complete test case :
Inherited entities definition :Code:
@Entity(name = "Location")
@Table(name = "LOCATION")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "DTYPE", discriminatorType = DiscriminatorType.STRING, length = 20)
public abstract class LocationEntity {
...
@Column(name = "CODE")
protected String code;
@OneToMany(mappedBy = "location")
protected List<LampEntity> lamps = new ArrayList<LampEntity>();
...
}
@Entity(name = "Floor")
@DiscriminatorValue(value = "floor")
public class FloorEntity extends LocationEntity {
...
@OneToMany(mappedBy = "floor")
private List<RoomEntity> rooms = new ArrayList<RoomEntity>();
...
}
@Entity(name = "Room")
@DiscriminatorValue(value = "room")
public class RoomEntity extends LocationEntity {
...
@ManyToOne
@JoinColumn(name = "OWNER_ID", nullable = false, referencedColumnName = "ID")
private FloorEntity floor;
...
}
Requested entity which is source of problem :Code:
@Entity(name = "Lamp")
@Table(name = "LAMP")
public class LampEntity {
@ManyToOne
@JoinColumn(name = "LOCATION_ID", referencedColumnName = "ID")
private LocationEntity location;
}
Spring data JPA repository used :Code:
public interface LampRepository extends JpaRepository<LampEntity, Long> {
@Query("SELECT lamp FROM Lamp lamp WHERE lamp.location.code = :locationCode AND TYPE(lamp.location) = Room")
public List<LampEntity> findAllByRoomCode(@Param("code") String locationCode);
...
}
The result is that the query findAllByRoomCode produces the following error :
Code:
org.hibernate.QueryException: could not resolve property: class of: xxx.yyy.LampEntity [SELECT lamp FROM xxx.yyy.LampEntity lamp WHERE lamp.location.code = :locationCode AND TYPE(lamp.location) = Room]
So I tried directly with the .class notation :
Code:
@Query("SELECT lamp FROM Lamp lamp WHERE lamp.location.code = :code AND lamp.location.class = Room")
This one works great and expected results are retrieved.
I have done an other test. I created a new repository, the LocationRepository and I tried this one :
Code:
@Query("SELECT location FROM Location location WHERE location.code = :code AND TYPE(location) = Room")
and it works perfectly.
My questions are : it is a bug from this version of hibernate or it isn't possible to do that ( TYPE(aaa.bb.ccc) )? If not possible and for my culture :), please why ?
I'm using spring-data-JPA 1.4.1.RELEASE with Hibernate 4.2.7.Final as provider.
Thanks a lot