2017-06-19 96 views
0

下面是我的命名查詢在這裏測試用例我的輸出列表重複 由於attribute.I需要一個OBJ這些都是屬性屬於同一測試案例如何獲得JPA集合元素命名查詢

「從測試用例選擇t.Id,ATR噸加入t.attributes作爲ATR」

public class TestCase { 

@javax.persistence.Id 
private Integer Id; 

@Column(name="ui_order") 
@OrderBy("uiorder desc") 
private Integer uiorder; 

@OneToMany(mappedBy="testCase") 
@OrderBy("name DESC") 
private Set<Attributes> attributes=new HashSet<Attributes>(); 
} 


public class Attributes { 

@Id 
private Integer Id; 

private String name; 

@ManyToOne 
@JoinColumn(name="test_case_id") 
private TestCase testCase; 

} 

查詢的輸出是

[["id":1,"attributes":{"name":"Battery","id":1}],["id":1,"attributes":{"name":"Fan","id":2}] 

所需的輸出是

[{"id":1,"attributes":[{"name":"Battery","id":1},{"name":"Fan","id":2}]}] 
+0

爲什麼有一個Integer字段上的「@ OrderBy」? '@ OrderBy'用於有序集合... –

+0

你不能,因爲結果將鏡像數據庫會給你什麼,這是表中每行的結果。如果你想要集合,你最好查詢TestCase(獲取連接屬性)並用你自己的toString方法自己顯示引用的集合。 – Chris

回答

0

嘗試以下操作:

SELECT DISTINCT t FROM TestCase t LEFT JOIN FETCH t.attributes 
0

好像要組由ID,所以你只需要添加GROUP BY

SELECT t.id, atr 
FROM TestCase t JOIN t.attributes as atr 
GROUP BY t.id 
+0

[{「id」:1,「attributes」:[{「name」:「Battery」,「id」:1}}]在給所有人分組後,只給予第一個屬性。 –

+0

你想要的輸出是什麼? – fg78nc

+0

[{「id」:1,「attributes」:[{「name」:「Battery」,「id」:1},{「name」:「Fan」,「id」:2}]}] –