2017-07-07 85 views
0

我很樂意在嵌套子映射集合上使用投影的任何幫助。
該代碼顯示了我想要實現的一切,特別是TestWrapper包含了一些實體列表。假設TestWrapper是我的DTO對象,正在積累其他對象。
我想從test3中獲取名稱並將其存儲爲僅包含該字段集的test3對象列表。
在開始讓我們來看看實體類,包裝和休眠投影代碼:Hibernate標準projection.OneToMany。嵌套DTO

@Entity 
public class Test { 
@Id 
@GeneratedValue(strategy = GenerationType.SEQUENCE) 
private Long id; 

private String name; 


@ManyToOne 
Test2 test2; 
@OneToMany(mappedBy = "test") 
List<Test3> tests3; 

public Test() { 
} 

public List<Test3> getTests3() { 
    return tests3; 
} 

public void setTests3(List<Test3> tests3) { 
    this.tests3 = tests3; 
} 

public Test(String name) { 
    this.name = name; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public Long getId() { 
    return id; 
} 

public void setId(Long id) { 
    this.id = id; 
} 

public Test2 getTest2() { 
    return test2; 
} 

public void setTest2(Test2 test2) { 
    this.test2 = test2; 
} 
} 



@Entity 
public class Test2 { 
@Id 
@GeneratedValue(strategy = GenerationType.SEQUENCE) 
private Long id; 

private String name; 

public Test2() { 
} 

public Long getId() { 
    return id; 
} 

public void setId(Long id) { 
    this.id = id; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 
} 

@Entity 
public class Test3 { 
@Id 
@GeneratedValue(strategy = GenerationType.SEQUENCE) 
private Long id; 

private String name; 
@ManyToOne 
@JoinColumn(name="test_fk") 
private Test test; 
public Test3() { 
} 

public Test getTest() { 
    return test; 
} 

public void setTest(Test test) { 
    this.test = test; 
} 

public Long getId() { 
    return id; 
} 

public void setId(Long id) { 
    this.id = id; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 
} 

包裝:

public class TestWrapper { 
private Test test; 

private String test2_name; 
private List<Test3> tests3; 
public TestWrapper() { 
} 

public TestWrapper(Test test, String test2_name) { 
    this.test = test; 
    this.test2_name = test2_name; 
} 

public List<Test3> getTests3() { 
    return tests3; 
} 

public void setTests3(List<Test3> tests3) { 
    this.tests3 = tests3; 
} 

public Test getTest() { 
    return test; 
} 

public void setTest(Test test) { 
    this.test = test; 
} 

public String getTest2_name() { 
    return test2_name; 
} 

public void setTest2_name(String test2_name) { 
    this.test2_name = test2_name; 
} 
} 

我的休眠標準投影:

public List<TestWrapper> getTestProjection() { 
    Session session = entityManager.unwrap(Session.class); 
    Criteria test = session.createCriteria(Test.class); 
    test.createAlias("test2", "test2", JoinType.LEFT_OUTER_JOIN); 
    test.createAlias("tests3","tests3",JoinType.LEFT_OUTER_JOIN); 
    test.setFetchMode("tests3", FetchMode.JOIN); 
    //  Criteria test2 = test.createCriteria("test2"); 
    ProjectionList projectionList = Projections.projectionList(); 
    projectionList.add(Projections.property("id")); 
    projectionList.add(Projections.property("test2.name")); 
    projectionList.add(Projections.property("test3.name")); 

    test.setProjection(projectionList); 
    test.setResultTransformer(new 
    AliasToBeanResultTransformer(TestWrapper.class)); 
    return test.list(); 
    } 

回答

0

你不能這樣做,您的查詢不會返回Entites,而只是像常規SQL查詢那樣的列。

AliasToBeanResultTransformer僅根據列的別名將每行映射到DTO的實例。

所以,你將不得不修改您的DTO,並給予和別名到您的預測寫自己的Transanformer重新集結行