I created new object, set its' fields with data and saved it.
So I now have record in DB that contains fields of my object and createdDate with date but modifiedDate with null value.
The problem is:
obj.Retrieve(); - ok obj fields are populated. but obj.m_modifiedDate has Now value.
obj.name = newName;
obj.Save(); - tries to save :UPDATE SculptureType SET nameRu=?, nameEn=?, createYear=?, meter=?, picture=?, patina=?, material=?, createdDate=?, modifiedDate=? WHERE SculptureType.id = ? AND SculptureType.createdDate = ? AND SculptureType.modifiedDate = ? ;
Got problem? modifiedDate in DB is null, but in obj it is Now.
So no records is updated.
What is wrong?
Here the Class code:
public class SculptureType : CPersistentObject
{
private long _id;
private string _nameRu;
private string _nameEn;
private string _createYear;
private string _meter;
private string _picture;
private string _patina;
private string _material;
#region Properties
public long id
{
get { return _id; }
set
{
if(_id!=value)
{
_id = value;
SetDirtyFlag();
}
}
}
public string nameRu
{
get { return _nameRu; }
set
{
if(!string.Equals(_nameRu, value))
{
_nameRu = value;
SetDirtyFlag();
}
}
}
public string nameEn
{
get { return _nameEn; }
set
{
if(!string.Equals(_nameEn, value))
{
_nameEn = value;
SetDirtyFlag();
}
}
}
public string createYear
{
get { return _createYear; }
set
{
if(!string.Equals(_createYear, value))
{
_createYear = value;
SetDirtyFlag();
}
}
}
public string meter
{
get { return _meter; }
set
{
if(!string.Equals(_meter,value))
{
_meter = value;
SetDirtyFlag();
}
}
}
public string picture
{
get { return _picture; }
set
{
if(!string.Equals(_picture,value) )
{
_picture = value;
SetDirtyFlag();
}
}
}
public string patina
{
get { return _patina; }
set
{
if(!string.Equals(_patina,value))
{
_patina = value;
SetDirtyFlag();
}
}
}
public string material
{
get { return _material; }
set
{
if(!string.Equals(_material, value))
{
_material = value;
SetDirtyFlag();
}
}
}
#endregion
public SculptureType()
{
//
// TODO: Add constructor logic here
//
//_id = 0;
}
public override CPersistentObject getNewObject()
{
return new SculptureType();
}
public override bool IsValid()
{
//TODO: this method should check object state is ready to be saved in database
return true;
}
SculptureType type = new SculptureType();
CPersistenceBroker pbroker = new CPersistenceBroker();
pbroker.init();
type.id = 4;
type.Retrieve();
type.nameRu = "New name";
type.Save(); - here I got zero records were updated exception
Thanks in advance,
Denis -SWE
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The problem was n handling nulls in query. After I have added to database config
<parameter name="ansinulls" value="true"/>
the update query gets correct IS NULL - null handling
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I created new object, set its' fields with data and saved it.
So I now have record in DB that contains fields of my object and createdDate with date but modifiedDate with null value.
The problem is:
obj.Retrieve(); - ok obj fields are populated. but obj.m_modifiedDate has Now value.
obj.name = newName;
obj.Save(); - tries to save :UPDATE SculptureType SET nameRu=?, nameEn=?, createYear=?, meter=?, picture=?, patina=?, material=?, createdDate=?, modifiedDate=? WHERE SculptureType.id = ? AND SculptureType.createdDate = ? AND SculptureType.modifiedDate = ? ;
Got problem? modifiedDate in DB is null, but in obj it is Now.
So no records is updated.
What is wrong?
Here the Class code:
public class SculptureType : CPersistentObject
{
private long _id;
private string _nameRu;
private string _nameEn;
private string _createYear;
private string _meter;
private string _picture;
private string _patina;
private string _material;
#region Properties
public long id
{
get { return _id; }
set
{
if(_id!=value)
{
_id = value;
SetDirtyFlag();
}
}
}
public string nameRu
{
get { return _nameRu; }
set
{
if(!string.Equals(_nameRu, value))
{
_nameRu = value;
SetDirtyFlag();
}
}
}
public string nameEn
{
get { return _nameEn; }
set
{
if(!string.Equals(_nameEn, value))
{
_nameEn = value;
SetDirtyFlag();
}
}
}
public string createYear
{
get { return _createYear; }
set
{
if(!string.Equals(_createYear, value))
{
_createYear = value;
SetDirtyFlag();
}
}
}
public string meter
{
get { return _meter; }
set
{
if(!string.Equals(_meter,value))
{
_meter = value;
SetDirtyFlag();
}
}
}
public string picture
{
get { return _picture; }
set
{
if(!string.Equals(_picture,value) )
{
_picture = value;
SetDirtyFlag();
}
}
}
public string patina
{
get { return _patina; }
set
{
if(!string.Equals(_patina,value))
{
_patina = value;
SetDirtyFlag();
}
}
}
public string material
{
get { return _material; }
set
{
if(!string.Equals(_material, value))
{
_material = value;
SetDirtyFlag();
}
}
}
#endregion
public SculptureType()
{
//
// TODO: Add constructor logic here
//
//_id = 0;
}
public override CPersistentObject getNewObject()
{
return new SculptureType();
}
public override bool IsValid()
{
//TODO: this method should check object state is ready to be saved in database
return true;
}
public override string ToString()
{
string pattern = "SculptureType [id={0}; nameRu={1}; nameEn={2}; createYear={3}; meter={4}; picture={5}; patina={6}; material={7};]";
return string.Format(pattern, new string[]{_id.ToString(), _nameRu, _nameEn, _createYear, _meter, _picture, _patina, _material});
}
}
Mapping:
<?xml version="1.0" encoding="utf-8" ?>
<map>
<database name="base" class="CMsAccessDatabase">
<parameter name="name" value="base.mdb" />
</database>
<class name="SculptureType" table="SculptureType" database="base">
<attribute name="id" column="id" key="primary" find="true" identity="true"/>
<attribute name="nameRu" column="nameRu" proxy="true"/>
<attribute name="nameEn" column="nameEn" proxy="true"/>
<attribute name="createYear" column="createYear" />
<attribute name="meter" column="meter" />
<attribute name="picture" column="picture" />
<attribute name="patina" column="patina" />
<attribute name="material" column="material" />
<attribute name="createdDate" column="createdDate" timestamp="true"/>
<attribute name="modifiedDate" column="modifiedDate" timestamp="true"/>
</class> .....
And Run Code:
SculptureType type = new SculptureType();
CPersistenceBroker pbroker = new CPersistenceBroker();
pbroker.init();
type.id = 4;
type.Retrieve();
type.nameRu = "New name";
type.Save(); - here I got zero records were updated exception
Thanks in advance,
Denis -SWE
The problem was n handling nulls in query. After I have added to database config
<parameter name="ansinulls" value="true"/>
the update query gets correct IS NULL - null handling