Spring Data JPA(Hibernate): How do I retrieve a concrete entity using only a field in its abstract superclass?

后端 未结 3 766
闹比i
闹比i 2020-12-31 09:08

Consider the following hierarchy, where entities WidgetA and WidgetB extend an abstract Widget superclass:

@Entity
@In         


        
3条回答
  •  半阙折子戏
    2020-12-31 09:32

    Hibernate supports that query just fine, and will happily return an instance of the correct subclass. Without knowing what it is in the Spring Data implementation that is trying to make an instance of Widget, you should be able to just declare the query and have it run it directly, rather than using the parser.

    public interface WidgetDAO extends JpaRepository {
    
        @Query("select w from Widget w where w.serialNumer = ?1")
        public Widget findBySerialNumber(String serialNumber);
    }
    

提交回复
热议问题