Java Spring数据JPA,hibernate子类属性排序顺序 spring数据jpa:1.5.2.0版本 休眠:4.3.5.4

Java Spring数据JPA,hibernate子类属性排序顺序 spring数据jpa:1.5.2.0版本 休眠:4.3.5.4,java,spring,hibernate,jpa,spring-data-jpa,Java,Spring,Hibernate,Jpa,Spring Data Jpa,在SpringDataJPA中,是否可以根据连接子类的属性进行排序,这就是我试图处理的查询 @Repository public interface InventoryItemRepository extends JpaRepository<InventoryItem<Item>, Long> { @Query("SELECT inventoryItem FROM InventoryItem inventoryItem JOIN TREAT (inventory

在SpringDataJPA中,是否可以根据连接子类的属性进行排序,这就是我试图处理的查询

@Repository
public interface InventoryItemRepository extends JpaRepository<InventoryItem<Item>, Long> {

    @Query("SELECT inventoryItem FROM InventoryItem inventoryItem JOIN TREAT (inventoryItem.item AS Sword) item WHERE inventoryItem.player = :player")
    List<InventoryItem<Item>> getSwordInventory(@Param("player") Player player, Pageable pageable);

}
这是hibernate试图执行的: 引发异常 实体 (为了演示,我已经将大部分内容剥离):

@实体
@继承(策略=InheritanceType.JOINED)
公共抽象类项{
@身份证
@GeneratedValue(策略=GenerationType.SEQUENCE)
长id;
字符串名;
}
@实体
公共级剑扩展项目{
整数攻击;
}
@实体
公共类盾牌扩展项{
整体防御;
}
@实体
公共类清单项{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
长id;
@许多酮
玩家;
@多通(targetEntity=Item.class)
T项;
整数金额;
}

上面使用的是JPA2.1,我还使用了以下JPA2.0查询,该查询给出了相同的错误@JPA 2.1规范(4.4.9)中的查询(“从inventoryItem inventoryItem中选择inventoryItem inventoryItem加入inventoryItem.item项目,其中inventoryItem.player=:player和type(item)=:type”)说明:“在FROM和WHERE子句的路径表达式中,支持使用TREAT运算符进行向下转换。使用TREAT运算符可以访问子类特定的状态。“因此在ORDER BY子句中可能不可用。我在使用type(item)=:type时也会得到相同的结果。对于JPA 2.0,仔细看,它会在item表上进行交叉连接,然后使用它进行排序。
Pageable pageable = new PageRequest(0, 10, new Sort(new Order(Direction.ASC, "item.name"))); // works as it matches on Item class
Pageable pageable = new PageRequest(0, 10, new Sort(new Order(Direction.ASC, "amount"))); // works as it matches on InventoryItem class
Pageable pageable = new PageRequest(0, 10, new Sort(new Order(Direction.ASC, "item.attack"))); // doesn't work, this is what I want to do
Hibernate:
select
    inventoryi0_.id as id1_0_,
    inventoryi0_.amount as amount2_0_,
    inventoryi0_.item_id as item_id3_0_,
    inventoryi0_.player_id as player_i4_0_ 
from
    InventoryItem inventoryi0_ 
inner join
    Item item1_ 
        on inventoryi0_.item_id=item1_.id 
inner join
    Sword item1_1_ 
        on item1_.id=item1_1_.id cross 
join
    Item item2_ 
where
    inventoryi0_.item_id=item2_.id 
    and inventoryi0_.player_id=? 
order by
    item2_1_.attack asc limit ?
Caused by: org.postgresql.util.PSQLException: ERROR: missing FROM-clause entry for table "item2_1_"
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Item {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    Long id;

    String name;
}

@Entity
public class Sword extends Item {
    Integer attack;
}

@Entity
public class Shield extends Item {
    Integer defence;
}

@Entity
public class InventoryItem<T extends Item> {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id;

    @ManyToOne
    Player player;

    @ManyToOne(targetEntity = Item.class)
    T item;

    Integer amount;
}