This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes How  to instabntiate the class through EJB QL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "How  to instabntiate the class through EJB QL " Watch "How  to instabntiate the class through EJB QL " New topic
Author

How to instabntiate the class through EJB QL

Rahul Ba
Ranch Hand

Joined: Oct 01, 2008
Posts: 205
I want to instantiat the constructor of the class through EJB QL.

I am trying
select new CListing() from CSummaryByDateWeek cdtwk where cdtwk.pk.Id = 1

Here,
CListing is a POJO.
CSummaryByDateWeek is an entity bean mapped to certain table.

when i tried to execute it gives me parse exception. at )

I am wondering is this execution possible in EJBQL?
I got this hint from
http://www.hibernate.org/hib_docs/entitymanager/reference/en/html/queryhql.html section 7.4

Let me know your opinion.

Thanks.
[ November 13, 2008: Message edited by: Rahul Ba ]
Reza Rahman
author
Ranch Hand

Joined: Feb 01, 2005
Posts: 580
    
    5
Rahul,

Take a look at JPQL constructor expressions using SELECT NEW. Most good JPQL references should cover it. Here is one: http://edocs.bea.com/kodo/docs41/full/html/ejb3_langref.html.

Regards,
Reza


Independent Consultant — Author, EJB 3 in Action — Expert Group Member, Java EE 6 and EJB 3.1
Rahul Ba
Ranch Hand

Joined: Oct 01, 2008
Posts: 205
Thanks a lot for the quick reply...

select new com.cList.CListing(cdtwk.sent) from CSummaryByDateWeek cdtwk where cdtwk.pk.Id = 1


Now my question is how to get the object of current construtor??
i.e CListing(cdtwk.sent)??

I want to return object of this class...

Thanks in advance.
[ November 13, 2008: Message edited by: Rahul Ba ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

unexpected token: )

I'm not sure it will work with the default constructor. What's the point of making new instances if you're not filling it with information taken from the database ? The specification does not clearly say anything about it though.


[My Blog]
All roads lead to JavaRanch
Rahul Ba
Ranch Hand

Joined: Oct 01, 2008
Posts: 205
Ok cool...

I got that, i did correction too.
My next question is

I using this query

select new com.CReport(cdtwk.cStartDate) " +
" from CByDateWeek cdtwk where cdtwk.pk.cId

but it's not finding constructor in class CReport class...

Data type cStartDate in CByDateWeek is Date

I have constructor in CReport class as

public CReport(Date sdate){
...
}

public CReport(Time sdate){
...
}

public CReport(TimeStamp sdate){
...
}

but nothing is working ..it says
Constructor not found exception.......what to more try???
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10240
    
168

Please post the complete exception stacktrace.


[My Blog] [JavaRanch Journal]
Rahul Ba
Ranch Hand

Joined: Oct 01, 2008
Posts: 205
02:16:41,265 ERROR [PARSER] Unable to locate appropriate constructor on class [com.CReport]
[cause=org.hibernate.PropertyNotFoundException: no appropriate constructor in class: com.CReport]

In more detail

Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor on class com.CReport.

Thanks in advance.
[ November 14, 2008: Message edited by: Rahul Ba ]
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10240
    
168

Please post the entity class CByDateWeek including the import statements and annotations used in that class.

While posting, remember to wrap it in a code block using the "Code" button in the message editor window.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Data type cStartDate in CByDateWeek is Date

java.sql.Date ? java.util.Date ?
Rahul Ba
Ranch Hand

Joined: Oct 01, 2008
Posts: 205

[ November 14, 2008: Message edited by: Rahul Ba ]
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10240
    
168

The query:
select new com.CReport(cdtwk.cStartDate)


The CReport constructor:

public CReport(String cname,Date sdate){
System.out.println("Hello :"+sdate);
name = cname;
}


You don't have a constructor which accepts just the date. As a result, you see

Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor on class com.CReport.


You need to fix the query or add a new constructor which accepts just the date.
Rahul Ba
Ranch Hand

Joined: Oct 01, 2008
Posts: 205
No I do have...

select new com.CReport(cdtwk.name,cdtwk.cStartDate) " +
" from CByDateWeek cdtwk where cdtwk.pk.cId

any guess??
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10240
    
168

Rahul,

If you look at your previous posts which contain the query, the code and the exception, they are completely contradicting each other. It becomes difficult to guess what's wrong. So PostRealCode and the actual exception stacktrace.
Rahul Ba
Ranch Hand

Joined: Oct 01, 2008
Posts: 205
Ok, Jaikiran

For simpliticity i posted that...Sorry for mis-understanding

Here is my HQL query
select new com.CReport(cdtwk.name,cdtwk.cStartDate) " +
" from CByDateWeek cdtwk where cdtwk.pk.cId



STACK TRACE

02:16:41,265 ERROR [PARSER] Unable to locate appropriate constructor on class [com.CReport]
[cause=org.hibernate.PropertyNotFoundException: no appropriate constructor in class: com.CReport]

In more detail

Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor on class com.CReport.

Thanks in advance.
Rahul Ba
Ranch Hand

Joined: Oct 01, 2008
Posts: 205
Thanks for the great help, people.

When I set util.Date in both class, I got the answer.
I am wondering why not for sql.Date?
[ November 14, 2008: Message edited by: Rahul Ba ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

In your entity class, did you try to set @TemporalType(DATE) to your date field ?
Rahul Ba
Ranch Hand

Joined: Oct 01, 2008
Posts: 205
Hi,

I am new to ejb3.
What is use of setting the @TemporalType(DATE) to date field?

Please explain.

Thanks in advance.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10240
    
168

Originally posted by Rahul Ba:
What is use of setting the @TemporalType(DATE) to date field?



In Java you have a java.sql.Date and a java.util.Date. The java.util.Date includes even the time part (ex: 2008-11-18 16:29:45) whereas the java.sql.Date does not include the time part (ex: 2008-11-18).

The @Temporal annotation is used to specify what level of precision you want with the Date type field. i.e. It allows you to specify whether you are interested only in the DATE part or only with the TIME part or both the date and the time (TIMESTAMP). So a @Temporal(TemporalType.DATE) tells that you are only interested in the Date part of the data stored in the column corresponding to this field.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to instabntiate the class through EJB QL