这篇博客探讨了在Hibernate中如何使用原生态SQL进行查询,并通过DTO(Data Transfer Object)进行数据传输。作者提供了几个示例,包括从SQL查询到对象转换的不同方法,如直接映射到自定义DTO类和使用Transformers.aliasToBean方法。同时,作者提出了使用Hibernate的最佳实践,建议在大项目中避免双向关联和尽量使用SQL查询而非HQL,以及在需要时自行处理缓存策略。
摘要由CSDN通过智能技术生成
package org.com.test.model1;
/**
* DTO对象没有任何存储的意义,仅仅是用来进行数据的传输的。
* 特别注意:这样的对象,必须有全部字段的构造方法和不带参数的构造方法。
* @author asus_n56
*
*/
public class StudentDto {
private Classroom classroom;
private Special special;
private Student student;
public Classroom getClassroom() {
return classroom;
}
public void setClassroom(Classroom classroom) {
this.classroom = classroom;
}
public Special getSpecial() {
return special;
}
public void setSpecial(Special special) {
this.special = special;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public StudentDto(Classroom classroom, Special special, Student student) {
super();
this.classroom = classroom;
this.special = special;
this.student = student;
}
public StudentDto() {
}
@Override
public String toString() {
return "StudentDto [classroom=" + classroom + ", special=" + special + ", student=" + student + "]";
}
}
package org.com.test.model;
/**
* DTO对象没有任何存储的意义,仅仅是用来进行数据的传输的。
* 特别注意:这样的对象,必须有全部字段的构造方法和不带参数的构造方法。
* @author asus_n56
*
*/
public class StudentDto {
private int stuId;
private String stuName;
pr