MySql Null DateTime and MS.net are not playing nice - mysql

Heres the issue.
I have a table(mySQL) that contains dates and some of them are null.
I know that I can use DateTime? or Nullable to allow nulls, but I'm not sure where to set it.
What I've tried:
I built the class in the dblm. The date propery has the following attributes set:
Nullable : True
Server Data Type : DateTime
Type : NUllable<DateTime>
These settings allow my code to be built.
When I debug it a get this exception thrown:
System.NullReferenceException: Object reference not set to an instance of an object..
If I try to with these attributes:
Nullable : True
Server Data Type : NUllable<DateTime>
Type : NUllable<DateTime>
I get the same exception as above.
Other ways that did not work:
Nullable : True
Server Data Type : DateTime?
Type : NUllable<DateTime>
Nullable : True
Server Data Type : DateTime?
Type : DateTime?
I'm using .net's 3.5 Framework
Thanks

I remember a problem with mysql's ADO.NET driver, where it was not playing nice with nulled date/time fields at all.
Even IsDBNull was throwing exception on a field with null date/time.
It may be still an issue.

Fixed -
It turned out to be an issue with the connection string to connect to the mySQL DB.
Also I didn't know that it was possible to drop a table into the DBML using the Server Explorer. All I needed to do was connect to the database then drop the table onto the DBML.

Related

JDO Class - convert to varchar or nvarchar based on MySQL or MSSQL

I have a JDO Class. Some of the attributes are as shown below:
#Column(jdbcType = "VARCHAR", length = 200)
String anotherSrcFieldValue;
#Column(jdbcType = "BIGINT")
long tgtFieldId;
#Column(jdbcType = "VARCHAR", length = 200)
String tgtFieldValue;
With MySQL and MSSQL it works fine.
My requirement is, if it is MySQL make it a column of type VARCHAR; and when it is MSSQL, create a column of type NVARCHAR. How can I achieve this?
A second requirement is one entity class to be run on both the databases.
All JDO docs I've seen explain clearly that putting schema specific info in annotations is a bad idea. Consequently you should have 2 files "package-mysql.orm" and "package-mssql.orm" to specify the schema-specific parts of the mapping, and set "datanucleus.Mapping" to be either "mysql" or "mssql" depending on your datastore. As per http://www.datanucleus.org/products/accessplatform_4_2/jdo/orm/metadata_orm.html

Grails property cannot be null error

My Grails app worked fine. If I remember corectly all I did was to change a contraint on one of my domain classes from blank: true, nullable:true to blank:false, nullable: false. And now when I try to create a new instance of that class I get following error messages:
Property [contract] of class [class com.app.Request] cannot be null
Property [description] of class [class com.app.Request] cannot be null
Property [productline] of class [class com.app.Request] cannot be null
Property [requestType] of class [class com.app.Request] cannot be null
Property [subject] of class [class com.app.Request] cannot be null
And I've provided values for all of these properties.
I'm guessing there is some conflict between gorm and database. Can someone explain to me whats going on and how to fix it.
I had similar problem, had updated constraints to non-null, but my stored data had nulls for non-null fields. At some point Grails tries to load existing data, but fails on validation step.
Try to update your database with some default values (I mean by using plain SQL UPDATE SET x = 'temp' WHERE x IS NULL), make grails clean and restart your app. Should help.

MySql Connector 6.5.4 - Stored Procedure Return Entity with a boolean field - Entity Framework

Searching, reading forums and all suggestion of SO before writing. (1 day already investigating the issue).
Im using: MySql Server 5.5 with Entity Framework 4.3 with Connector 6.5.4
(I was using connector 6.3.6 and everything worked perfectly, updated and problem occurs)
I have a SP that returns an List of an Entity Object. That Entity have a bool (tinyint(1)) field BUT when using the SP it returns it as string.
I created a temporary table and return that but the same problems occurs. The error is:
System.InvalidOperationException: The 'isDeleted' property on 'Container' could not be
set to a 'String' value. You must set this property to a non-null value of type
'Boolean'. at
System.Data.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.
GetValue(DbDataReader reader, Int32 ordinal)
Problem is, since it's a Entity object, i cannot Convert.ToBoolean() [also i dont want to].
I think the problem is that i don't have the ability to cast the SP field as bool or the connector has a bug (most likely).
As I said, it worked with no problem with connector 6.3.6
Thanks.
The bug is there but I found a workaround. The problem happend when the order of the fields aren't the same.
This BUG: http://bugs.mysql.com/bug.php?id=53166 helped me to understand and try making the select order of fileds the same the table.
Example:
If table is :
field_a, field_b, field_c
and your stored procedures returns: field_b, field_a, field_c won't work.
Changed my SP to return field_a, field_b, field_c

Tell Hibernate's hbm2ddl to add MySQL enum columns for #Enumerated annotated fields

I'm creating a DB table using hbm2ddl with Java code similar to the following:
#Entity
public class Filter {
public enum Type {
TypeA, TypeB;
}
#Enumerated(EnumType.STRING)
private Type type;
}
It works fine, but for "type" a VARCHAR column is created, i.e. the DDL code looks like this:
CREATE TABLE IF NOT EXISTS `filter` (`type` varchar(255) DEFAULT NULL)
But what I want to have is this:
CREATE TABLE IF NOT EXISTS `filter` (`type` enum('TypeA','TypeB') NOT NULL)
Is this possible to declare in Hibernate, preferred with annotations?
Or is there a way to extend SchemaUpdate and overwrite the method that renders the alter script part for enumerated field the way I like it?
Background: The same database is used in a PHP part of the project and I want to prevent that invalid values are inserted.
Although it seems there is no way to handle MySQL enums 100% automatically, as pointed out by Lucas on his answer, there is actually a simple way to contour it. You may use columnDefinition attribute on #Column annotation, which seems to be specifically designed to generate custom DDL code.
See the documentation excerpt describing the attribute:
(Optional) The SQL fragment that is used when generating the DDL for the column.
Defaults to the generated SQL to create a column of the inferred type.
The NOT NULL restriction is quite standard, and is supported by another attribute nullable.
Thus, your property definition would look like this:
#Enumerated(EnumType.STRING)
#Column(columnDefinition = "enum ('TypeA', 'TypeB')", nullable = false)
private Type type;
I believe that's going to be complicated, since the java.sql.Types, which define the sql types treated by java, does not have enum type (since it's not a standardized type according to SQL-92).
If that was the case you could create a hibernate custom UserType extending the EnumType and setting the sqlType accordingly, but since java.sql.Types doesn't handle it I don't see how to use native sql enum.
best regards!

BLToolKit: how to fetch 'empty' datetime field?

Request Execute is failed if one of fields to be mapped has DateTime field and corresponding value in DB has '0000-00-00' or '0001-01-01'. The following error is returned
Unable to convert MySQL date/time value to System.DateTime
Is there any possibility to fetch such value?
I've tried to specify the 'DateTime?' value as property type - it doesn't help too (actually, I didn't expect that to be helpful).
P.S. I use MySql 5.1
I came across a similar problem using NHibernate with the same error in an exception.
It's due to MySQL's unique "feature" of allowing invalid dates in a DATE field, especially using 0000-00-00 as a default value for DATE NOT NULL columns. When such a date is encountered, it throws an exception when converting itself to a DateTime.
The suggested solution for this was to add
Allow Zero Datetime=True;
to the connection string, however in practice this did not work for me. I eventually solved the problem by altering the connection string adding
Convert Zero DateTime=true;
so your app.config section would look something like this
<connectionStrings>
<add
name="ConnectionString.MySql"
connectionString="Server=localhost;Port=3306;Database=BLT;Uid=someuser;Convert Zero DateTime=true;"
providerName="MySql.Data.MySqlClient"/>
Have you tried the MapValue attribute? I'm not sure if this will work but...
[MapValue(null, "0000-00-00")]
[MapValue(null, "0001-01-01")]
public DateTime? theDate;
i think you have to control it by another property.
[MapField("the_date")]
public DateTime? theDate; // Map
[MapIgnore]
public DateTime theDateControl
{
set {
if(theDate.HasValue)
{
....
}
}
}