gpt4 book ai didi

java - 使用 Transformer 将实体映射到 DTO 时获取实体内的集合

转载 作者:太空宇宙 更新时间:2023-11-04 11:06:35 25 4
gpt4 key购买 nike

我有一个名为 Student 的实体

@Entity
@Table(name = "students")
public class Student implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "STUDENT_ID")
private Integer studentId;

@Column(name = "STUDENT_NAME", nullable = false, length = 100)
private String studentName;

@OneToMany(fetch = FetchType.EAGER, mappedBy = "student", cascade = CascadeType.ALL)
private List<Note> studentNotes;

// Some other instance variables that are not relevant to this question

/* Getters and Setters */

}

和一个名为 Note 的实体

@Entity
@Table(name = "notes")
public class Note implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "NOTE_ID")
private Integer noteId;

@Column(name = "NOTE_CONTENT")
private String noteText;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "STUDENT_ID")
private Student student;

/* Getters and Setters */

}

正如您所看到的,该关系表明学生可以拥有多个笔记。

为了在特定页面上显示有关学生的一些信息,我只需要学生姓名、笔记计数和所有笔记。我为此创建了一个 StudentDTO,它看起来像这样:

public class StudentDTO {

private Long count;
private String name;
private List<Note> notes;

/* Getters and setters */

}

我使用以下代码将从数据库返回的 Student 和 Notes 映射到 StudentDTO

private static void testDTO() {
Session session = getSessionFactory().openSession();
String queryString = "SELECT count(n) as count, s.studentName as name, s.studentNotes as notes " +
"from Student s join s.studentNotes n where s.id = 3";
Query query = session.createQuery(queryString);

List<StudentDTO> list = query.setResultTransformer(Transformers.aliasToBean(StudentDTO.class)).list();
for (StudentDTO u : list) {
System.out.println(u.getName());
System.out.println(u.getCount());
System.out.println(u.getNotes().size());
}
}

当查询中获取注释时,上述代码会失败,但如果我删除注释并仅获取名称和计数,则它可以正常工作。

当查询中包含注释时,Hibernate 会引发以下错误:

select
count(studentnot2_.NOTE_ID) as col_0_0_,
. as col_3_0_,
studentnot3_.NOTE_ID as NOTE_ID1_2_,
studentnot3_.NOTE_CONTENT as NOTE_CON2_2_,
studentnot3_.STUDENT_ID as STUDENT_3_2_
from
students studentx0_
inner join
notes studentnot2_
on studentx0_.STUDENT_ID=studentnot2_.STUDENT_ID
inner join
notes studentnot3_
on studentx0_.STUDENT_ID=studentnot3_.STUDENT_ID
where
studentx0_.STUDENT_ID=3;

这是我收到的错误消息:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as col_3_0_, studentnot3_.NOTE_ID as NOTE_ID1_2_, studentnot3_.NOTE_CONTENT as N' at line 1

现在我可以看到查询错误的地方,但它是由 Hibernate 生成的,而不是我可以控制的。我的查询字符串中是否需要更改某些内容才能获得我需要的结果。

我不想手动将结果映射到我的 DTO,有没有办法可以直接将 Student.java 中的 StudentNotes 映射到 StudentDTO.java 中的笔记

最佳答案

看起来这个查询是错误的。更好的方法是只找学生。您随时可以从学生那里获得笔记集。

Session session = getSessionFactory().openSession();
String queryString = from Student s where s.studentId = 3;
Query query = session.createQuery(queryString);
Student student = query.getSingleResult();
sysout(student.getNotes().size())

此外,我从未在 SELECT 子句中以这种方式检索集合;所以,不确定但你真的需要

join s.studentNotes

在您的查询中?不确定我的回答是否有帮助。

关于java - 使用 Transformer 将实体映射到 DTO 时获取实体内的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46418390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com