【问题标题】:Hibernate criteria projection.OneToMany. Nested DTOs休眠标准投影。OneToMany。嵌套 DTO
【发布时间】:2017-12-11 03:55:13
【问题描述】:

我很高兴在嵌套子映射集合上使用投影的任何帮助。
该代码显示了我想要实现的一切,特别是 TestWrapper 包含一些实体的列表。让我们假设 TestWrapper 是我的 DTO 对象累积其他。
我想从 test3 获取例如名称并将其存储为仅包含该字段集的 test3 对象列表。
首先让我们看一下实体类、包装器和休眠投影代码:

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

【问题讨论】:

    标签: java hibernate projection hibernate-criteria


    【解决方案1】:

    你不能这样做,你的查询不返回实体,而只是像常规 sql 查询一样的列。

    AliasToBeanResultTransformer 只是根据列的别名将每一行映射到一个 DTO 实例。

    所以你将不得不修改你的 DTO 并为你的投影赋予别名编写你自己的 Transanformer 来重新组合行

    【讨论】:

      猜你喜欢
      • 2011-04-11
      • 2015-07-17
      • 1970-01-01
      • 1970-01-01
      • 2012-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-25
      相关资源
      最近更新 更多