简体   繁体   中英

Hibernate criteria projection.OneToMany. Nested DTOs

I would be glad for any help with using projections on nested child mapped collections.
The code shows everything I want to achieve especially TestWrapper contains list of some entities. Let's assume that TestWrapper is my DTO object accumulating other.
I want to get from test3 for example name and store it as list of test3 objects containg only that field set.
At beginning let's look at entity classes,wrapper and hibernate projection code :

@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;
}
}

wrapper:

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;
}
}

And my hibernate criteria projection:

    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();
   }

You can not do that, your query is not returning Entites, but just columns like a regular sql query.

The AliasToBeanResultTransformer just map each rows to an instance of DTO based on the alias of the column.

So you will have to modify your DTO and give and alias to your projections OR write your own Transanformer to regroup row

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM