PersistentLocalDateTimeAsString results in DateTimeParseException - spring

In the hibernate model when using jadira-usertype org.jadira.usertype.dateandtime.threeten.PersistentLocalDateTimeAsString as Type, the conversion is not reversible.
#Column(name="requested_start")
#Type(type="org.jadira.usertype.dateandtime.threeten.PersistentLocalDateTimeAsString")
private LocalDateTime requestedStartDate;
When creating object with datetime in following format;
myObject.setRequestedStartDate(LocalDateTime.parse("2014-12-28T19:30:00"));
is stored in DB (MariaDB) as "2014-12-28T19:30" ignoring the seconds part (don't know why).
when querying back the data, I'm getting the following exception
java.time.format.DateTimeParseException: Text '2014-12-28T19:30' could not be parsed at index 16
But, If I set date as "2014-12-28T19:30:01" with seconds set to "01", it is working fine.
I also tried setting the springframework's DateTimeFormat, still facing the same exception.
#Column(name="requested_start")
#Type(type="org.jadira.usertype.dateandtime.threeten.PersistentLocalDateTimeAsString")
#DateTimeFormat(pattern="yyyy-MM-dd'T'HH:mm:ss")
private LocalDateTime requestedStartDate;

You can try this modified pattern "yyyy-MM-dd'T'HH:mm[:ss]".
The meaning of the square brackets is indicating an optional part during parsing - see javadoc.

Related

SQlite NOT throwing exceptions for unknown columns in where clause

A little background: I am building an App on Laravel 5.6.33 (PHP 7.2 with sqlite 3).
So i have this weird case where in a test I am expecting an Exception but it never gets thrown. So I went digging and found that Laravel does not throw exceptions for invalid/non existent columns in where clause if the database driver is sqlite. The following code just returns an empty collection instead of throwing an exception.
\App\Tag::where('notAColumn', 'foo')->get();
Its weird and I checked all over the place to see if it was something wrong with my config and found nothing out of place. Debug is set to true etc. Im running this code for testing the app using an in memory sqlite database.
One other thing I noticed was that if I use whereRaw instead of where, exceptions are thrown as expected. so for example the following throws an exception.
\App\Tag::whereRaw('notAColumn = "foo"')->get();
Does anyone know why this maybe?
The difference between your two queries is the (non-)quoting of the column name:
Tag::where('notAColumn', 'foo')->get();
// select * from "tags" where "notAColumn" = 'foo'
Tag::whereRaw("notAColumn = 'foo'")->get(); // Literals are wrapped in single quotes.
// select * from "tags" where notAColumn = 'foo'
From the documentation:
If a keyword in double quotes (ex: "key" or "glob") is used in a context where it cannot be resolved to an identifier but where a string literal is allowed, then the token is understood to be a string literal instead of an identifier.
So SQLite would interpret Tag::where('notAColumn', 'notAColumn')->get(); as a comparison of two (identical) strings and therefore return all rows in the table.

How to parse Integer value as keys in YAML File

I am running a spring boot project where I need to parse an erro code dynamically, but I have realised that if the key is of integer type, it return null. Is there a way to parse Integer Keys in YAML file
Parsing This property doesnt work
errorcode:
00001: An error occurred whilst trying to process your request. We would like to apologise for the inconvenience.
Howver Parsing This property work
errorcode:
ONE: An error occurred whilst trying to process your request. We would like to apologise for the inconvenience.
I am trying to read this property using
import org.springframework.core.env.Environment;
#Autowired
private Environment environment;
error= environment.getProperty("errorcode.ONE") Works
error= environment.getProperty("errorcode.00001") --Doesn't works
So in nutshell, if an integer has been used as a key, YAML file is returning value as null. One workaround is to use the key this way as shown below, but any better ideas?
error
code-001:
It can be solved by specifying that number as string in yaml. like,
errorcode:
'00001': An error occurred whilst trying to process your request. We would like to apologise for the inconvenience.
This worked for me.
Read all string from the yaml and replace the integer value with non integer value. Descerialize the class with non integer value as variable.

ConstraintViolationException - extract field name which caused exception

I'm using hibernate-validator with a JAX-RS service to validate query parameters using #NotNull:
#GET
public Response doSomething(#NotNull #QueryParam("myParam") String myParam) {...}
This works as expected and throws a ConstraintViolationException if myParam is null. I'd like to extract the param name which is associated to the violation (e.g. myParam), and return that in the response message to the client but there does not appear to be an obvious way of extracting this from the exception. Can someone provide some insight?
As of BeanValidation 1.1 there is a ParameterNameProvider contract which makes parameter name extraction configurable. As mentioned in the other answer, with Java 8 you can get the parameter names in the byte code provided you compile with the -parameters flag. Use the ReflectionParameterNameProvider in this case. However, even with Java 7 you can get parameter names, for example by using the ParanamerParameterNameProvider. This parameter name provider is based on Paranamer and there are several ways to set it up.
This only works if you're using Java 8, as prior to Java 8 the actual parameter name was lost at compile time. Its now retained, assuming you compile and run at Java 8. See also http://docs.jboss.org/hibernate/validator/5.2/reference/en-US/html_single/#_java_8_support

Exception DateTime OleDbParameter

I’m using Visual Studio 2010. Within the project we add a DataSet, inside it; we have a Query Table Adapter to do all the queries to a SQL Server 2000 Data Base. One of the queries is formed using a Stored Procedure that receives four parameters. One of the parameters is a DateTime data type. Although we have check many times, we are receiving an unexpected exception:
Provider encountered an error while sending command parameter[0] '' value and stopped processing.
Conversion failed for command parameter[1] '' because the data value overflowed the type used by the provider.
Provider encountered an error while sending command parameter[2] '' value and stopped processing.
Provider encountered an error while sending command parameter[3] '' value and stopped processing.
Provider encountered an error while sending command parameter[4] '' value and stopped processing.
Working around this, if we delete the DateTime parameter of the Store Procedure, the query executes successfully otherwise we get the exception mentioned before.
We notice that the DateTime parameter has the property set as follows:
DbType: DateTime
ProviderType: DBTimeStamp
Any approach trying to accomplish the execution of the Query will be greatly welcome.
When I received this error, I had to change my date parameter to tell what the data type was.
Old Way
cmd.Parameters.Add(new OleDbParameter("TDate", DateTime.Now));
New Way
OleDbParameter dateParam = new OleDbParameter("TDate", OleDbType.Date);
dateParam.Value = DateTime.Now;
cmd.Parameters.Add(dateParam);

SQLite with WebMatrix.WebData.SimpleMembershipProvider Long to Int Cast Error

I'm trying to use the SimpleMembershipProvider with SQLite to enable authentication on my site. I have it initialized in my Global.aspx and can create users, however I can't log users in. I get the following error when I try to call WebSecurity.Login([UserName], [Password]):
Cannot implicitly convert type 'long' to 'int'. An explicit conversion exists (are you missing a cast?)
The stack trace is as follows (after I make the call to the Login function:
CallSite.Target(Closure , CallSite , Object ) +146
System.Dynamic.UpdateDelegates.UpdateAndExecute1(CallSite site, T0 arg0) +661
WebMatrix.WebData.SimpleMembershipProvider.VerifyUserNameHasConfirmedAccount(IDatabase db, String username, Boolean throwException) +315
WebMatrix.WebData.SimpleMembershipProvider.ValidateUser(String username, String password) +162
WebMatrix.WebData.WebSecurity.Login(String userName, String password, Boolean persistCookie) +32
I know that SQLite only allows Int64 values in numeric columns (that aren't float). The question is: how do I get the SimpleMembershipProvider to work with Int64 values? I had a look at the source code for the offending routines listed in the stack trace and it seems that all numbers are being explicitly cast before being returned from their Int32 functions. However, I don't have the source to System.Dynamic.UpdateDelegates.UpdateAndExecute1 or CallSite.Target, so I can't see what is going on there. What is more, those are probably pre-compiled. So, the question remains, how can I get this to work?
The SimpleMembership Provider only supports SQL Server and SQL Compact 4.0. SQLite is not supported. If you want to use SQLite, you will have to create your own implementation of ExtendedMembershipProvider. You can more or less copy SimpleMembershipProvider.cs, and just modify the bits where the data type conflict arises.

Resources