0
我已經走了兩個實體與雙向onetomany關聯。我想使用帶有投影的標準(僅從少數列中獲取數據)獲得許多帶有許多Adress
的許多Person
。查詢結果我想放入List<Map<String, Object>>
。結果列表應該有這樣的結構(地址數據應在父地址對象List<Map<String, Object>>
包含):準則,預測,ALIAS_TO_ENTITY_MAP
[
{
name: "Andrew"
surname: "..."
adress : [
{
city : "New York"
street : "..."
type : "home"
}
{
city : "..."
street : "..."
type : "work:
}
]
{
name: "..."
surname : "..."
address : [{....}]
}
]
實體:
public class Person{
private Long id;
private String name;
private String surname;
private Integer age;
private String phoneNr;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "person", fetch = FetchType.LAZY)
private List<Address> addresses
// getters and setters
}
public class Address{
private Long id;
private String city;
private String street;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="person_id")
private Person person;
// getters and setters
}
所有我能做到是加載整個實體,哪些不符合我的需求。
Criteria criteria = currentSession().createCriteria(Person.class);
criteria.setFetchMode("addresses", FetchMode.JOIN);
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
任何建議將是非常有益的。
可以從查詢中返回一個List