Java – JPA, custom query and date

I am faced with a strange problem My search includes stack overflow and JPA and custom queries. I should specify parameters So I have a query string because I have more than 14 fields, but I have a date problem I always get IllegalStateException

INFO: query STRING = SELECT t FROM Tickets t  WHERE t.startdate > :startDate AND t.enddate < :endDate ORDER BY t.status DESC
WARNING: #{ticketController.search}: java.lang.IllegalStateException: Query argument startDate not found in the list of parameters provided during query execution.

As for my inquiry:

Query q = em.createQuery(query).setParameter("startDate",startDate,TemporalType.TIMESTAMP).setParameter("endDate",endDate,TemporalType.DATE);

Although the parameter I got was not found, I have it in setparameter and set it in the query I see in the info line

Any ideas?

Thank you in advance

Edit:

INFO: query STRING = SELECT t FROM Tickets t  WHERE t.startdate > ?1 AND t.enddate < ?2 ORDER BY t.status DESC
WARNING: #{ticketController.search}: java.lang.IllegalStateException: Query argument 1 not found in the list of parameters provided during query execution.

q = em.createQuery(query).setParameter(1,TemporalType.TIMESTAMP).setParameter(2,TemporalType.TIMESTAMP);

In addition, according to the suggestion, I have checked that the date I use is Java util. Date. In the entity class, I have timestamp But I still can't do it, and I'm not sure where I failed

Just to ensure that everything is what they should be, I forced the query to be a string, and I got the correct exception:

INFO: query STRING = SELECT t FROM Tickets t  WHERE t.startdate > :startDate AND t.enddate < :endDate ORDER BY t.status DESC
WARNING: #{ticketController.search}: java.lang.IllegalArgumentException: You have attempted to set a value of type class java.lang.String for parameter startDate with expected type of class java.util.Date

But then again, I changed my appointment and failed: s I checked the reason for this IllegalStateException:

From debugging and Javadoc, I get the following: getresultlist

IllegalStateException – if the Java persistence query language update or delete statement is called

I didn't update or delete:/

Edit 2: add entity related parts:

@Basic(optional = false)
@NotNull
@Column(name = "startdate")
@Temporal(TemporalType.TIMESTAMP)
private Date startdate;
@Column(name = "enddate")
@Temporal(TemporalType.TIMESTAMP)
private Date enddate;

For as of database creation script, columns are being created as follows:

startdate timestamp with time zone NOT NULL,endate timestamp with time zone,

If I make a normal SQL query, such as "select * from tbl_tickets where StartDate > '2012-02-01 00:00:00' and enddate < '2013-03-18 23:59:50'", I get the ideal result I think I can use native queries, but it will solve the problem, not the problem, right? Edit 3: Although I have set all the contents correctly, the init of the bean calls the query without args again (sorry, thank you for your help. It helps me check what is wrong)

Solution

Two JavaDocs

setParameter(String name,java.util.Date value,TemporalType temporalType)`
setParameter(String name,java.util.Calendar value,TemporalType temporalType)`

Status:

Since you did not provide the complete code, please verify:

>The Java value StartDate is of type Java util. Date or Java util. Calendar.> The SQL column StartDate has a valid SQL date type timestamp

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>