-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: How to map the JPA or Hibernate createQuery result to a DTO
PostPosted: Thu Aug 24, 2017 7:48 am 
Newbie

Joined: Tue Apr 25, 2017 12:26 am
Posts: 4
I am firing the HQL Join query

Code:
@Override
public List<Object> getCourseIdWithStatus(int courseId, String status) {

    String queryString = "select sc.courseId, sc.name, sct.streamId from Course "
            + "sc join CoursesTypeInformation sct ON(sc.courseId = sct.courseId)"
            + " where sc.courseId = ?1 and sc.status = ?2 and sct.status = ?3";
    Query query = entityManager.createQuery(queryString);
    query.setParameter(1, courseId);
    query.setParameter(2, status);
    query.setParameter(3, status);
    return (List<Object>)query.getResultList();
}


This is a HQL Query not a native SQL

Now I have a pojo class

Code:
public class BasicData {

    private int courseId;

    private String name;

    private int streamId;

    .. setters and getters

}


I want to map result of query to POJO. WHat should I use?


Top
 Profile  
 
 Post subject: Re: Hibernate createQuery resultset Mapping(Spring boot)
PostPosted: Thu Aug 24, 2017 8:51 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Use SELECT NEW like this:

Code:
String queryString = "select new my.package.name.BasicData(sc.courseId, sc.name, sct.streamId) from Course "
            + "sc join CoursesTypeInformation sct ON(sc.courseId = sct.courseId)"
            + " where sc.courseId = ?1 and sc.status = ?2 and sct.status = ?3";

Query query = entityManager.createQuery(queryString);
    query.setParameter(1, courseId);
    query.setParameter(2, status);
    query.setParameter(3, status);
    return (List<BasicData>)query.getResultList();



Top
 Profile  
 
 Post subject: Re: Hibernate createQuery resultset Mapping(Spring boot)
PostPosted: Fri Aug 25, 2017 1:37 am 
Newbie

Joined: Tue Apr 25, 2017 12:26 am
Posts: 4
This is working. But this expects that BasicData class has a constuctor with the above agruments.
Now lets say there are 20 columns that I want to fetch from DB, so I have to make a constrctor with these 20 arguments.

Is there any way I can do it without creating a parameterized constructor?


Top
 Profile  
 
 Post subject: Re: Hibernate createQuery resultset Mapping(Spring boot)
PostPosted: Fri Aug 25, 2017 2:00 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Of course, there is an alternative. What else would you expect from an awesome tool like Hibernate, right?

If you don't like the constructor-based approach, then you should use one of Hibernate's ResultTransformer which works with fields or setters, whatever it can find on the entity.

Code:
String queryString = "select sc.courseId as courseId , sc.name as naame, sct.streamId as streamId) from Course "
            + "sc join CoursesTypeInformation sct ON(sc.courseId = sct.courseId)"
            + " where sc.courseId = ?1 and sc.status = ?2 and sct.status = ?3";

return (List<BasicData>) entityManager.createQuery(queryString)
    .setParameter(1, courseId);
    .setParameter(2, status);
    .setParameter(3, status);
    .unwrap(org.hibernate.query.Query.class)
    .setResultTransformer(Transformers.aliasToBean(BasicData.class))
    .getResultList();


Top
 Profile  
 
 Post subject: Re: How to map the JPA or Hibernate createQuery result to a DTO
PostPosted: Fri Aug 25, 2017 2:23 am 
Beginner
Beginner

Joined: Sat May 21, 2011 7:40 am
Posts: 22
Or you give Blaze-Persistence Entity Views a shot which handles all the DTO specifics for you.
If you map the "courseType" relation in the entity

Code:
@Entity
class Course{
  @ManyToOne(optional = false)
  @JoinColumn(name = "courseId")
  CourseTypeInformation courseType;

  // ...
}


.. the code with Blaze-Persistence Entity Views could look like this:

The DTO/Entity View definition
Code:
@EntityView(Course.class)
interface BasicData {
  int getCourseId();
  String getName();
  @Mapping("courseType.streamId")
  int getStreamId();
}


And the usage

Code:
CriteriaBuilderFactory cbf = //... see setup about how to create that
EntityViewManager evm = //... see setup about how to create that

// Your base query
CriteriaBuilder<Course> cb = cbf.create(entityManager, Course.class);
cb.where("courseId").eq(courseId)
  .where("status").eq(status)
  .where("courseType.status").eq(status);

// Applying projections independently
List<BasicData> data = evm.applySetting(EntityViewSetting.create(BasicData.class), cb);


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.