0

I have an entity call Student with few fields related to student and a list of Subject with oneToMany relation. Here i need to get all the fields including the List of Subject except "image" as it uses more memory it takes long time to retrieve all rows. Can some one say how to create Projection and criteria to retrieve this Student object without image field alone? Is there any custom result transformer like "AliasToBeanNestedResultTransformer"? As this will not work for my scenario where i am having List of Subject as OneToMany relationship.

@Entity @Table(name="STUDENT") public class Student { @id private long studentId; private String name; private String dob; private int age; @Lob private byte[] image; @Lob private byte[] imageTnail; @OneToMany(mappedBy="subject", cascade = CascadeType.ALL, orphanRemoval=true) private List<Subject> subjects; //setter & getter... } @Entity @Table(name="SUBJECT") public class Subject { @id private long subjectId; private String subjectName; @ManyToOne @JoinColumn(name="studentId") private Student student; } 

Edit

List<Student> results = getSession().createCriteria(Student.class) .setProjection( Projections.projectionList() .add( Property.forName("studentId")) .add( Property.forName("name") ) .add( Property.forName("dob")) .add( Property.forName("age")) ).setResultTransformer(Transformers.aliasToBean(Student.class)).list(); System.out.println("StudentDaoImpl.getStudents()"+results.get(0).getName()); 

2 Answers 2

1

Try this solution:

List<T> results = session.createCriteria(Student.class) .setProjection( Projections.projectionList() .add( Property.forName("studentId"),"studentId" ) .add( Property.forName("name"),"name" ) .add( Property.forName("dob"),"dob" ) .add( Property.forName("age"),"age" ) ).add( Restrictions.eq("studentId", @idAsParam ) ) .setResultTransformer(Transformers.aliasToBean(Student.class); .list(); 
Sign up to request clarification or add additional context in comments.

2 Comments

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
Additionally, at first glance it seems like a copy-paste from the other answer (I see the difference now). Perhaps it might be valuable to note why the other answer didn't work for you and why you made the changes that you did.
0

First of all, mappedBy attribute must hold the name of the foreign key so you ll have to do this correction :

@OneToMany(mappedBy="student", cascade = CascadeType.ALL, orphanRemoval=true) private List<Subject> subjects; 

Secondly, YES you can retrieve a list of student without getting their images :

List<T> results = session.createCriteria(Student.class) .setProjection( Projections.projectionList() .add( Property.forName("studentId") ) .add( Property.forName("name") ) .add( Property.forName("dob") ) .add( Property.forName("age") ) ) .setResultTransformer(Transformers.aliasToBean(Student.class); .list(); 

if you wish to get a specific Student

List<T> results = session.createCriteria(Student.class) .setProjection( Projections.projectionList() .add( Property.forName("studentId") ) .add( Property.forName("name") ) .add( Property.forName("dob") ) .add( Property.forName("age") ) ).add( Restrictions.eq("studentId", @idAsParam ) ) .setResultTransformer(Transformers.aliasToBean(Student.class); .list(); 

The first object will hold the object.

3 Comments

This works but i am getting subjects as null. I should get all subject associated to each student.
so you ll have to add it in your projection : .add( Property.forName("subjects") )
Added code above in question. I am seeing Student object returned, but all the fields are having null value. Is that something i am missing?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.