12

I have a class as following that need to retrieve from DB using Hibernate. The problem is my class has multiple members and majority of them are classes, how can I retrieve them?

@Entity
public class Student {
  @Id
  long id;
  String name;
  String fname;
  @OneToMany
  List<Course> courses;
  @ManyToOne
  Dealer dealer;
  ...
}

@Entity
public class Dealer {
   @Id
   long id;
   String name; 
   @OneToMany(fetch = FetchType.LAZY, mappedBy = "cr.dealer", cascade = CascadeType.ALL)
   Set<Car> cars = new HashSet<Cars>(0);
   ..

}

I need to retrieve student id 1 and all its courses, its dealer and list of dealers' cars.

My projection is as following but it does not return anything.

  ...
    .setProjection(Projections.projectionList()

    .add(Projections.property("friends.cars").as("cars")
    ...
Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
Jack
  • 6,430
  • 27
  • 80
  • 151

4 Answers4

4
    // Projection is not needed, Hibernate will load child values as shown below

    Student student = session.get(Student.class);
    List<Course> courses = student.getCourses();
    Dealer dealer = student.getDealer();

    //  If u want records only where child records are present, u can use LEFT_OUTER_JOIN

    Criteria criteria = getHibernateSession().createCriteria(Student.class);
    criteria.createAlias("Course", "Course", JoinType.LEFT_OUTER_JOIN);

    // If u want to use Projections for performance, u have to add each and every column in projection

    Criteria criteria = getHibernateSession().createCriteria(A.class);
    criteria.createAlias("b", "b", JoinType.INNER_JOIN);
    criteria.createAlias("b.r", "b.r", JoinType.INNER_JOIN);
    criteria.createAlias("b.c", "b.c", JoinType.LEFT_OUTER_JOIN);
    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.groupProperty("column1"));
    projectionList.add(Projections.property("column2"));
    projectionList.add(Projections.property("column3"));
    criteria.setProjection(projectionList);
    criteria.setResultTransformer(Transformers.aliasToBean(Table.class));
madhu pathy
  • 429
  • 2
  • 6
  • Thanks for your answer, I have a new issue with this and posted new question for that issue please have a look thanks, http://stackoverflow.com/questions/29980421/how-to-retireve-a-set-list-of-member-objects-using-hibernate – Jack May 01 '15 at 01:35
1

Because you have a List of Courses and a Set of Cars, you can simply fetch the whole graph in a single query:

select s
from Student s
left join fetch s.courses
left join fetch s.dealer d
left join fetch d.cars
where s.id = :id

Because you are fetching two collections, this query will generate a Cartesian Product, so you need to make sure that the selected children collections don't have too many entries.

If you don;t want to run into a Cartesian product, you can simply run this query:

select s
from Student s
left join fetch s.courses
left join fetch s.dealer d
where s.id = :id

and then you access the dealer.cars to fetch that collection with a separate query:

Student s = ...;
s.getDealer().getCars().size();
Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
  • Thanks for your answer, would you please have a look at my question at http://stackoverflow.com/questions/29980421/how-to-retireve-a-set-member-objects-using-hibernate – Jack May 31 '15 at 03:05
0

If high performences are not a concern, then you should let Hibernate do his work. Just use the getters of you entities. For exemple:

Student student1 = session.get(Student.class, 1L);
List<Course> courses = student1.getCourses();
Dealer dealer = student1.getDealer();
Set<Car> cars = dealer.getCars();
grebesche
  • 511
  • 1
  • 3
  • 14
0

I am not sure if you can use QueryOver but it would be very easy for these kind of tasks.

Student student = null;
Dealer dealer = null;
Course course = null;
Car car = null;

var myStudent = Session.QueryOver<Student>(() => student)
.Left.JoinQueryOver(() => student.courses, () => courses)
.Left.JoinQueryOver(() => student.dealer, () => dealer)
.Left.JoinQueryOver(() => dealer.cars, () => car)
.SelectList(list => list
  .Select(() => student.Name)
  .Select(() => student.Age)
  .Select(() => courses.Description)
  .Select(() => dealer.locaiton)
  .Select(() => car.Model))
  .TransformUsing(Transformers.AliasToBean<StudentModel>())
  .List<StudentModel>().AsQueryable();

Create a StudentModel DTO to have the results. This is just a hint to start with, you can modify this according to your requirement. I hope this will work. :)

Builder
  • 1,046
  • 2
  • 10
  • 30