-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: select query with JPA TYPE expression on entity field
PostPosted: Wed Jul 09, 2014 9:18 am 
Newbie

Joined: Wed Jul 09, 2014 8:46 am
Posts: 2
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.