I'm using Hibernate Search 4.4.0 in Infinispan. I'm trying to search the date of inserting docs. And I have saved date in type String.
This is the definition of date in my class:
private String ins = Constants.dateFormat.format(new Date());
And the dateFormat is:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Now I mapping this field like:
mapping.entity(Entity.class)
.property("ins", ElementType.FIELD).field().analyze(Analyze.NO);
I have verified that there are objects in my cache whose date property is 2012-09-17 14:28:32.0. But when I use query like:
Query query = queryBuilder.keyword().onField("ins").matching("2012-09-17").creatQuery();
to search in cache, there is no result.
Is there something I messed up??
I'm not an expert in Hibernate Search, but I think that instead of
.analyze(Analyze.NO)
you shall write
.analyze(Analyze.NOT_TOKENIZED)
or alike
Related
I make an application based on hibernate,
I just wanna ask,
How to parse this "31/10/13" to Oracle date in Java,
before this, I've try this method
SimpleDateFormat sdf1 = new SimpleDateFormat("dd/MM/yyyy");
Date log_date = sdf1.parse(tgl1);
it works, but when I try to make a simple query to search data like this
select * from coreservice where log_date='31-OCT-13';
but I got no data found in result....
Any help will be pleasure :)
Use oracle to_date functionin your query as follows:
select * from coreservice where log_date = to_date('31-OCT-2013','dd/mon/yyyy');
I have a pojo class in which one of the field is date. Here I am using hibernate to insert values into the db using these pojos.
I have set the current date value for this property and I am inserting the value to the DB. Here I need to generate the insert script programaticaly. I have done this and i am printing the insert statement in the console. But while printing in the console the date is shown as Fri Jun 07 04:49:07 ACT 2013 and the insert statement is
INSERT INTO tables (dates)values('Fri Jun 07 04:49:07 ACT 2013');
I don't want to generate the script like this i need it as
INSERT INTO tables (dates)values('2013-06-07');
I know We can use simple date formatter but i need this as date to set the POJO value. So if it is String it will not be set into the object.
I am forming the query as below
StringBuffer columnName = new StringBuffer();
columnName.append("insert into Tables values ('"+obj.getdates()+"')");
Before i used logger and at that time the query was formed and i think hibernate took care of that formatting because after inserting the query was formed as
INSERT INTO tables (dates)values('2013-06-07');
But now by hardcoding it is giving the above query whcih is not getting executed as the date value is not correct.
Can anyone help me here. Also in the case of hibernate whether formatting is done by hibernate or at the backend whether it is converting automatically
Thanks
convert your date into java.sql.Date
java.util.Date utilDate = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
System.out.println("utilDate:" + utilDate);
System.out.println("sqlDate:" + sqlDate);
I am trying to upload the data from a delimited text file to the lotus notes form using java agent. The issue arises when I try to insert the date value to the notes document. After insert when i use ComputeWithForm, then it returns false. I am using simpledateformat to format the date in MM/dd/yyyy format, but it is still not working. Below is the excerpt from my code.
String delim, key, thekey, myDate;
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy);
myDate = dateFormat.format(date);
newdoc.replaceItemValue("UploadDBDate", myDate);
Any help will be great.
Thanks,
Himanshu
myDate is a String object. The replaceItemValue method does not know that you have put a date into that String, therefore it treats it as ordinary text. If uploadDBDate is a DateTime field, that causes a type mismatch during the computeWithForm operation.
The Lotus classes for Java include a DateTime class. The Session class has a createDateTime method that you pass a "mm/dd/yyyy" string and return a DateTime object. Then you can pass that DateTime object into replaceItemValue instead of passing in myDate.
I would recommend you to do those things:
1) disable computewithform and simply save document and then verify field UploadDBDate, does it have correct value? does it have corrrect type?
2) if everything is fine with UploadDBDate then there is a problem on a form, so try to investigate what calculation you do on the form, because problem is there.
I am trying to insert into a variable in MS- SQL database the current date and the time.
I use this format:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime()));
and I get this as a result 2013-01-28 09:29:37.941
My field in the database is defined datetime and as I have seen in other tables which have the same field, the date and the time is written exactly like this 2011-07-05 14:18:33.000.
I try to insert into the database with a query that I do inside a java program, but I get this error
SQL Exception: State : S0003 Message: The conversion of a varchar
data type to a datetime data type of the value is out of range. Error
: 242
My query is like that:
query = "INSERT INTO Companies CreatedOn"+
"VALUES ('" + dateFormat.format(cal.getTime()) + "')"
but I don't understand what I am doing wrong.
According to the error description, you are inserting an incorrect type into the database. See JDBC to MSSQL. You should convert Calendar to Timestamp.
Try using:
PrepareStatement statement
= connection.prepareStatement("INSERT INTO Companies CreatedOn VALUES(?)");
java.sql.Timestamp timestamp = new java.sql.Timestamp(cal.getTimeInMillis());
statement.setTimestamp(1, timstamp);
int insertedRecordsCount = statement.executeUpdate();
First of all, do NOT use string concatenation. Have you ever heart about SQL injection?
Correct way how to do that is to use prepared statement:
Idea is you define statement with placeholders and than you define value for those placeholders.
See #Taky's answer for more details.
dateFormat#format this method returns formatted string not Date object. Database field is DateTime and it is expecting java.sql.Timestamp to be inserted there not String according to docs.
To conform with the definition of SQL DATE, the millisecond values
wrapped by a java.sql.Date instance must be 'normalized' by setting
the hours, minutes, seconds, and milliseconds to zero in the
particular time zone with which the instance is associated.
Try java.sql.Timestamp object instead of String in query and I'd recommend you to use PreparedStatement.
This is because you are trying to save String date value to Date type DB field.
convert it to Data dataType
You can also use the datetime "unseparated" format yyyymmdd hh:mm:ss
You could use Joda framework to work with date/time.
It maps own date/time types to Hibernate/SQL types without problem.
When you set parameters in HQL query joda carries about right type mapping.
If you want to store current date and time then you should use MYSQL inbuilt method NOW().
for brief documentation refer http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html . so your code will be like.
INSERT INTO Companies CreatedOn VALUES(NOW())"
However If you want to do it using java Date-util then it should be
Calendar cal = Calendar.getInstance();
java.sql.Timestamp timestamp = new Timestamp(cal.getTimeInMillis());
In my JSP page I have a field which is date and when I getting as request.getParameter("dateVal"); gives me
15-Dec-2012 12:21.
I would like to pass this value to my database procedure and insert/update into table.
How can I pass the value as setDate using prepareCall to database?
Thanks
First step would be using SimpleDateFormat to parse it to a fullworthy java.util.Date instance in the controller:
Date date = new SimpleDateFormat("dd-MMM-yyyy HH:mm.", Locale.ENGLISH).parse(dateVal);
Then you can just create a java.sql.Date around its time in the database layer:
statement.setDate(1, new java.sql.Date(date.getTime()));
Unrelated to the concrete problem, please note that java.sql.Date doesn't remember the time part. If you have actually a DATETIME or TIMESTAMP field in the DB and not a DATE field, then rather use setTimestamp() with a java.sql.Timestamp instead. This way the time part will also be stored.
#BalusC 's answer is perfect. But as an alternative solution you can use the function provided by database to convert String to Date while querying. For example(in case you use Oracle),
to_date(date_in_String, format)
Try this :
new SimpleDateFormat("dd-MM-yyyy HH:mm").parse(mydate);