のPOJOのリストを含むPOJOに休止状態ResultSetを変換します)は、私はこれに似たPOJOを持って
ここに私が持っているコードのスニペットがあります:
String query = "SELECT
distinct student.id as \"studentId\",
student.name as \"studentName\",
student.email as \"studentEmail\"
FROM
sample.sample_Student student";
Query q = entityManager.unwrap(Session.class).
createSQLQuery(query).
setResultTransformer(Transformers.aliasToBean(StudentResultPojo.class));
return q.list();
JPQLとは対照的に、NativeSQLでクエリを保持したいと考えています。 編集:クエリはいつでも変更できます。私はdbにクエリを格納し、必要に応じて編集したいと思います。ネイティブクエリの場合は、テストが簡単になります。
JPQLには、JPAクエリの結果をpojoに変換するのと同様の機能があります。select new(Pojo.class.getName()....)のようなコンストラクタを使用していますが、これは直面している問題と同じです。
私が持っているものは、私のサービスが返すStudentResultPojoリストをループしなければならないため、データベースからデータを取得するか、またはHibernateをナビゲートしてStudentResultPojoごとにcoursePojoListを埋めなければなりません関係には時間がかかる。
エンティティは次のようになります。
Student{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Long id;
private String name;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "student")
private Set<Course> courses = new HashSet<>(0);
}
Course{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Long id;
private String name;
private String time;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "student_id", referencedColumnName = "pk")
@ForeignKey(name = "student_course")
private Student student;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "course")
private Set<Teacher> teacher;
}
......
質問:私は2行に結果を返すクエリがある場合(未JSONをクエリが実行されたときに、ちょうど結果が返される)
1, James, [email protected], Physics, Albert, 9.00, 7887897899, [email protected]
1, James, [email protected], English, Jackie, 10.00, 7887097899, [email protected]
されていますCourseResultPojosのリストを使って結果を単一のStudentResultPojoに変換する方法がありますか?私StudentResultPojoが
1, James, [email protected] for id, name and email respectively
としての属性値を持つことになりますし、リストが
1st Object values as Physics, Albert, 9.00, 7887897899, [email protected] for attributes name, teacher, time,teacherPhone,teacherEmail
2nd Object values as English, Jackie, 10.00, 7887097899, [email protected] for attributes name,teacher,time,teacherPhone,teacherEmail
で2つのCourseResultPojoオブジェクトを持つことになります意味
ありがとうございました。
似たような質問がありましたが、ここでは答えられませんでした:http://stackoverflow.com/questions/13656054/hibernate-can-i-return-a-non-mapped-list-of-objects-that-contains-a-list ?rq = 1 – StudentForever