我有一個像這樣的表(MySql)
(ID(primary-key), Name, rootId)
(2, asdf, 1)
(3, sdf, 1)
(12, tew, 4)
(13, erwq, 4)
現在我想選擇資料庫中存在的不同根 tsg_ids。在這種情況下,它應該回傳 1,4。
我試過了
List<Entity> entityList = Entity.find(SELECT DISTINCT t.rootId from table t).list().
在除錯模式下,我看到物體串列包含(“1”,“4”)。Entity.find() 只能被帶入“物體”物件,但我從選擇查詢中得到的是字串。因此,在這種情況下,我無法將 Entity 物件轉換為 String 物件。
有沒有辦法使用 PanacheEntity 獲取非主列的不同值?
uj5u.com熱心網友回復:
我不知道您是使用帶有 Hibernate Reactive還是帶有 Hibernate ORM的 Panache ,但是,目前,如果您想使用 Panache,則必須使用投影:
@RegisterForReflection
public class EntityRootIdView {
public final Long rootId;
public EntityRootIdView(Long rootId){
this.rootId = rootId;
}
}
// Panache with Hibernate ORM
List<EntityRootIdView> rootIds = Entity
.find("SELECT DISTINCT t.rootId from Entity t")
.project(EntityRootIdView.class)
.list()
// Panache with Hibernate Reactive
Uni<List<EntityRootIdView>> rootIds = Entity
.find("SELECT DISTINCT t.rootId from Entity t")
.project(EntityRootIdView.class)
.list()
請注意,這至少需要 Quarkus 2.12.0.Final
或者,您可以使用 Hibernate Reactive 會話:
Uni<List<Long>> rootIds = Panache.getSession()
.chain(session -> session
.createQuery("SELECT DISTINCT t.rootId from Entity t", Long.class)
.getResultList() )
);
或者,如果您使用的是 Hibernate ORM,物體管理器:
List<Long> rootIds = Panache.getEntityManager()
.createQuery( "SELECT DISTINCT t.rootId from Entity t", Long.class )
.getResultList();
有趣的是,我剛剛創建了一個問題來使這更容易。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/522268.html
上一篇:一次更新mysql中的多個列
下一篇:使用游標從視圖中輸出多列
