nred : Insulting people is easy, I don’t think so I am since, its easy for you guys to read 577 pages of JPA specifications, because you r the team who creates Hibernate and maintain it to align with default Java specification, its your task, I am as of now a user, just looking help from creators of API.
Also in process to create my desired output I reached where this exception is threw by hibernate
when I used source code instead of hibernate jar
org.hibernate.hql.internal.ast.QuerySyntaxException: Unable to locate appropriate constructor on class [com.journaldev.spring.persistence.EmployeeBean]. Expected arguments are: java.lang.String, java.lang.String [select new com.journaldev.spring.persistence.EmployeeBean(generatedAlias0.empId, generatedAlias0.empName) from com.journaldev.spring.persistence.EmployeeBeanas generatedAlias0 where generatedAlias0.rntContact=:param0]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:92) ~[main/:?]
at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:110) ~[main/:?]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:288) ~[main/:?]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:200) ~[main/:?]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:142) ~[main/:?]
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:127) ~[main/:?]
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:81) ~[main/:?]
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:151) ~[main/:?]
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:276) ~[main/:?]
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:214) ~[main/:?]
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1913) ~[main/:?]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:483) [main/:?]
at org.hibernate.jpa.criteria.CriteriaQueryImpl$1.buildCompiledQuery(CriteriaQueryImpl.java:335) [main/:?]
but my doubt is that, what is causing Hibernate to seek for constructor based initialization because I wanted to initialize the object by getter & setter methods of its fields.
If you can help me out so I can build direct initialization of bean instead of returning Object array while using javax.persistence.criteria.Selection in CriteriaQuery.multiselect method.
Because possibly you guys and Java also suggests that for any specific requirement there must be specific DTO which should only have those specific fields but I deffer that openion since if I have already class(Pojo or bean) I will defiantly use that for reusability for example:
I have a table EMPLOYEE which have emp_id, manager_id, emp_name, emp_dept, emp_address columns
so its equivalent bean will have Employee entity or pojo or bean now in that case if I want only in my selection emp_id and manager_id then I will be forced to create another DTO class EmployeeDTO which will have constructor EmployeeDTO(empId, managerId)
this actually against re-usability since I have already Employee class and for each new requirements I have to create new DTO class it creates unnecessary boilerplate code and barred developer to achieve reusability of bean as data carrier
possibly you are referring below code snippet as your solution but its not helping since I have to create a DTO class with parameterized constructor
public List dbFind3(EmployeeSearchCriteria sc) throws Exception
{
* CriteriaQuery<Object[]> query= getCriteriaBuilder().createQuery(Object[].class);// query object
Root<EmployeeBean> mappingBean= query.from(EmployeeBean.class);
List<Predicate> predicateList= SQLExpression(sc); //returns Predicate List
Predicate predicate= null;
if(sc.isAND())
predicate= criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
else
predicate= criteriaBuilder.or(predicateList.toArray(new Predicate[predicateList.size()]));
query= query.where(predicate);
query.multiselect(new Selection[]{mappingBean.get("empId"),mappingBean.get("empName")});
** List<Object[]> list= entityManager.createQuery(query).getResultList();
return list;
}
My requirement was like below
Check * : CriteriaQuery<EmployeeBean> query= getCriteriaBuilder().createQuery(EmployeeBean.class);// query object
And at **: List<EmployeeBean> list= entityManager.createQuery(query).getResultList();
Here inside EmployeeBean there is no constructor defined and its exact mapping class having fields more than what I am trying to select in above code snippet.
Now I have no wonder what solution you are talking about, even I will read JPA specs it will not help because I don’t know your API internal working, for which I am requesting your help
Now I request help me instead of of insulting me.Hope you are able to connect what I am talking above.