Map Java Boolean to bit in SQL Server - sql-server

I am trying to map a Boolean variable in java with a bit column (nullible )in SQL Server. I can see the record getting updated in DB but the status value is not updated . Do i need to change the data type to anything else . please suggest.
My pojo element looks like below.
#Column(name = "status")
#Override
public Boolean getStatus() {
return super.getStatus();
}

Related

Update records using EntityFrameworkCore

I'm new to using EF to handle data in SQL. In a MVC Core project we're testing EF (Microsoft.EntityFrameworkCore, version 2.2.3) to handle data.
When trying to update data and update failed for some reason (missing fields etc) it seemed like EF actually deleted the record from the database (MSSQL 2014) instead of throwing an update error...
Is it possible?
Code for updating:
public void Update(Contact contact)
{
_dbContext.Update(contact);
_dbContext.SaveChanges();
}
When trying to update data and update failed for some reason (missing fields etc) it seemed like EF actually deleted the record from the database (MSSQL 2014) instead of throwing an update error...
Is it possible?
It should not.
test it, try to debug here
_dbContext.Update(contact);
_dbContext.SaveChanges();
var updated = _dbContext.Contacts.FirstOrDefault(x => x.Id == contact.Id); //debug here
check if it has a value, if still none, these are the scenarios i can think of that may have caused your problem
investigate the missing field specially if it is not nullable.
is the _dbContext used here is the same connection string used with everything?
is the [Key] attribute listed on your Contact entity?
public class Contact
{
[Key]
public int Id
}
overridden the SaveChanges function?
is what you are passing Contact contains a Key and it is not 0?
is a delete function called after Update?
try using SQL Profiler to look at the Linq to SQL if it really generated an update query and if it is really pointing at the right [Key]
but if it is still not working properly, you could do
public void Update(Contact contact)
{
var selectedContactToBeUpdated = _dbContext.Contacts.FirstOrDefault(x => x.Id == contact.Id);
if (selectedContactToBeUpdated != null)
{
selectedContactToBeUpdated.PropertyToBeUpdated1 = newValue;
selectedContactToBeUpdated.PropertyToBeUpdated2 = newValue2;
//additional Properties
_dbContext.SaveChanges();
}
}
in the scenario above, it will only generate an Update statement with fields you have changed.

Dapper FluentMap: property with name 'Date' can't be mapped

I've faced the problem with Dapper FluentMap (version 1.60) which I don't know how to fix. It looks like Dapper can't map a column from a table if the corresponding property in .NET class has "Date" name.
I have the table in DB (I'm using MS SQL if that's matter) with column Dt of Date type.
There is an entity with DateTime property:
public class MyEntity {
public DateTime Date { get; set; }
}
and the corresponding mapping:
public class MyEntityMapper : EntityMap<MyEntity> {
public MyEntityMapper() {
Map(p => p.Date).ToColumn("Dt");
}
}
When I try to get data from the DB and map to MyEntity, I get the following error:
ArgumentNullException: Value cannot be null. Parameter name: meth
If I rename Date property in MyEntity to something else like Dt or JustDate - everything works fine. Is there such restriction in Dapper Fluent Map (not allowed to give a name to property equal to data type name in DB)?
If so, is it possible to overcome it somehow? Because in my case it's a bit problematic to rename property in MyEntity
Yes you are right, the only solution you can do was to change your entity property Date into other name.

iBatis unable to read property from Map when using isEqual

I'm seeing a very bizarre issue with iBatis when trying to read a property from a Java map using isEqual, but not with other iBatis operators. For example it is able to read the map properties fine when using isNotNull and iterate. The xml:
<isNotNull property="filterCriteria.account">
AND
id
<isEqual property="filterCriteria.account.meetsCriteria" compareValue="false">
NOT
</isEqual>
IN
(SELECT DISTINCT id
FROM account
WHERE some other criteria....
)
</isNotNull>
The 2 java classes we're using here:
public class SearchProfile {
private Map<String, SearchProfileCriteria> filterCriteria;
public SAOSearchProfile() {
filterCriteria = new HashMap<>();
}
public Map<String, SAOSearchProfileCriteria> getFilterCriteria() {
return filterCriteria;
}
public void setFilterCriteria(Map<String, SAOSearchProfileCriteriaBase> filterCriteria) {
this.filterCriteria = filterCriteria;
}
}
Above is the container object that is passed to iBatis for the querying, and below is the criteria object that will be the value of the map. In this example it is keyed with the String "account"
public class SearchProfileCriteria {
boolean meetsCriteria;
public String getCriteriaAsString() {
return StringUtils.getStringValueFromBoolean(meetsCriteria);
}
public boolean isMeetsCriteria() {
return meetsCriteria;
}
public void setMeetsCriteria(boolean meetsCriteria) {
this.meetsCriteria = meetsCriteria;
}
public String getSQLString(){
return meetsCriteria ? "" : "NOT";
}
}
And the exception:
Cause: com.ibatis.common.beans.ProbeException: There is no READABLE property named 'account' in class 'java.util.Map'; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
The getSQLString() method was my half baked attempt at a work around, the String gets escaped in the query and throws a syntax error.
When I remove the <isEqual> block the query executes find, which indicates it is able to read the "account" key when checking the to see if it is null. As I mentioned above, we're also able to use the map keys in <iterate> tags without issue. It seems <isEqual> and <isNotEqual> are the only tags causing issues. Does anyone have experience with this or know what may be going on?
Beware: Using isNotNull, isEqual, iterate is iBatis, they don't exist anymore in Mybatis, so referencing to Mybatis indifferently is confusing.
Reference documentation.
For your issue, how does it behave if replacing Map with a class (property will be known at compile time)?
Or try using <isPropertyAvailable>.
The work around could work with right syntax: $ instead of #: $filterCriteria.account.SQLString$ instead of #filterCriteria.account.SQLString#, then the value is just concatenated instead of bound as parameter.

StoredProcedureItemReader Spring Batch setting param values

I am using StoredProcedureItemReader in Spring Batch for Reading Items from a DB via Stored Procedure (Which accepts input parameters).
I have done setting the basic configurations for StoredProcedureItemReader but Not getting how to set the parameters values in it.
StoredProcedureItemReader storedProcItemReader = new StoredProcedureItemReader();
storedProcItemReader.setDataSource(dataSource);
storedProcItemReader.setProcedureName("proc_name");
SqlParameter[] parameter = {new SqlParameter(OracleTypes.VARCHAR),new SqlParameter(OracleTypes.VARCHAR),new SqlParameter(OracleTypes.CURSOR)};
storedProcItemReader.setParameters(parameter);
storedProcItemReader.setPreparedStatementSetter(??)
I want to set the values for two input parameters via PreparedStatementSetter. How do i set it. Do i need to use a preparedstatement for it. As i have already given the proc name (which has all the query to execute).
Thanks
You need to use an ItemPreparedStatementSetter :
public class MyItemPreparedStatementSetter implements ItemPreparedStatementSetter<T> {
#Override
public void setValues(T item, PreparedStatement ps) throws SQLException {
//Set your values here, example :
ps.setString(1, item.getProperty());
}
}
Statement fields are 1-indexed.
Then you can pass it to your reader :
storedProcItemReader.setPreparedStatementSetter(new MyItemPreparedStatementSetter());

Manually assign value to a hibernate UUID

As we know, in hibernate, configure the generator of a id to "uuid" , then hibernate will auto generate a UUID value to the id field when saving a new object.If configuring the generator to "assigned", the id must be assigned a value before saving a object.
And I found that if configuring the generator to uuid and assigning the id a value manually, the hibernate will change the value to a new UUID one.
My question is, when the generator is configured as uuid, how to manually assign a value to it?
PS: I use spring HibernateDaoSupport to save.
org.springframework.orm.hibernate3.support.HibernateDaoSupport.save(Ojbect obj)
Thanks!
If you need it only in rare special cases, the simpliest way is to issue INSERT queries in native SQL instead of using save().
Alternatively, you can customize generator to achieve the desired behaviour:
public class FallbackUUIDHexGenerator extends UUIDHexGenerator {
private String entityName;
#Override
public void configure(Type type, Properties params, Dialect d)
throws MappingException {
entityName = params.getProperty(ENTITY_NAME);
super.configure(type, params, d);
}
#Override
public Serializable generate(SessionImplementor session, Object object)
throws HibernateException {
Serializable id = session
.getEntityPersister(entityName, object)
.getIdentifier(object, session);
if (id == null)
return super.generate(session, object);
else
return id;
}
}
and configure Hibernate to use it by setting its fully qualified name as strategy.

Resources