Why use and instead of or in mybatis to check a empty stirng or null - mybatis

I have 2 conditions (m_name is emptystirng and m_name is null)
so, I wanted input sql query only m_name has a value.
but
<if test="m_name !=''or m_name !=null">
above setenece is not worked.
so I found below and then it's worked
update menu01
<set>
<if test="m_name !=''and m_name !=null">
m_name = #{m_name}
</if>
but I cannot understand why using 'and'.
because if it is null. it cannot be emptystring because it is null.
please let me know.
sorry for my english.
please please help me.
thank you

Related

JPA #Query - Need to tell null from non-null and build query string accordingly

I have the following query:
#Transactional
#Modifying
#Query(value = "INSERT INTO c_mark(c_fk, a_e_mark) values(:c_fk, :a_e_mark\\:\\:character varying[])", nativeQuery = true)
int insertClassMark(#Param("c_fk") #Nullable String classification_fk, #Param("a_e_mark") #Nullable String a_e_mark);
I am having an issue that requires I check whether a_e_mark is null before I attempt the cast above. If its null, put NULL, if not, put :a_e_mark\\:\\:character varying[]. So, I attempted the following on the query:
#Query(value = "INSERT INTO c_mark(c_fk, a_e_mark) values(:c_fk, :a_e_mark is null or :a_e_mark\\:\\:character varying[])"
but that didn't work because according to postGres:
argument of OR must be type boolean, not type a_e_mark[] . So, I guess everything after the OR is not interpreted as boolean?
If that is correct, I would be grateful if someone could help me make this:
:a_e_mark is null or :a_e_mark\\:\\:character varying[]
interpreted as a boolean result? Grateful for any ideas. Thanks

select query builder returning null

I'm new to Laravel and I'm trying to retrieve the id value of a selected row using query builder but it's not working. I've tried it in many ways according to the Laravel documentation and I still have a problem. I think it's related to the use of a variable but I don't know how to fix it.
public function submit_idea(Request $request)
{
$key=$request->input('key');
$workshop_id= DB::table('workshops')->where('autokey',$key)->value('id');
$id = auth()->User()->id;
$idea=new Idea;
$idea->title=$request->input('title');
$idea->description=$request->input('description');
$idea->user_id=$id;
$idea->workshop_id=$workshop_id;
$idea->save();
return view('submit_idea');
}
the error i'm getting is:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'workshop_id' cannot be null (SQL: insert into ideas (title, description, user_id, workshop_id) values (ppp, iiuu, 7, ?))
Can anyone help me, please?
Change:
$workshop_id = DB::table('workshops')->where('autokey',$key)->value('id');
To:
$workshop_id = DB::table('workshops')->select('id')->where('autokey',$key)->first();
By the way, the errors means that the workshop_id can't be null. If it can be null, be sure to add nullable() to the column in your migration file.
You can also working with
$workshopId = DB::table('workshops')->where('autokey', $key)->first()->pluck('name');
echo $workshopId;

JPA Criteria "IS NULL OR NOT IN"

I am trying to build the CriteriaQuery equivalent of the following SQL where clause, using OpenJPA 2.4.0:
where employee_status is null or employee_status not in ('Inactive','Terminated')
I have created a list of employeeStatuses, and this is how I am adding the predicate:
predicates.add(
criteriaBuilder.or(
criteriaBuilder.isNull(domainEntity.get("employeeStatus")),
criteriaBuilder.not(domainEntity.get("employeeStatus").in(employeeStatuses))
)
);
The generated SQL looks like this:
AND (t0.EMPLOYEE_STATUS IS NULL OR NOT (t0.EMPLOYEE_STATUS = ? OR t0.EMPLOYEE_STATUS = ?) AND t0.EMPLOYEE_STATUS IS NOT NULL)
As you can see, the 'not in' statement is being transformed into multiple comparisons, with 't0.EMPLOYEE_STATUS IS NOT NULL' added at the end. In order for this to work, the translated clause should be contained in another set of parenthesis.
Any ideas about how I can get this to work?

How to iterate List in MyBatis

iBatis to MyBatis Migration:
Need Help for MyBatis foreach logic, because the Map contains Value as ArrayList.
The below java code is the logic:
employeeRequest.put("ID", employeeId);
Map <String,ArrayList<String> employeeRequest = new HashMap<String, ArrayList<String>>();
Set<String> employeeSet = new HashSet<String>();
for(Employee employee: employeeList) {
String name = employee.getName();
String value = employee.getValue();
if("EMPLOYEE".equalsIgnoreCase(name) {
employeeSet.add(value)
}
}
if(!employeeSet.isEmpty()) {
employeeRequest.put("EMPLOYEE", new ArrayList<String>(employeeSet))
}
iBatis SQL:
My Previous code I am using iBatis which has the following query
<select id="getEmployeeName" resultclass="java.util.HashMap" parameterClass="java.util.Map">
SELECT EMP.EMPNAM NAME FROM EMPLOYEE EMP
WHERE EMP.ID = #ID#
<isNotEmpty property="EMPLOYEE" prepend="AND">
<iterate property="EMPLOYEE" conjunction="AND">
EMP.EMPNAM != #EMPLOYEE[]#
<iterate>
</isNotEmpty>
</select>
MyBatis SQL:
Now I am migrating to MyBatis, so formatted the query as below
<select id="getEmployeeName" resultclass="java.util.HashMap" parameterClass="java.util.Map">
SELECT EMP.EMPNAM NAME FROM EMPLOYEE EMP
WHERE EMP.ID = #{ID}#
<if test="EMPLOYEE !=null and EMPLOYEE>0">
<foreach collection="EMPLOYEE" index="index" item="item" separator="AND">
EMP.EMP_ID != ${item}
</foreach>
</if>
</select>
Could any one of you please help me with the correct query for the above java code logic.
Missing spaces around separator value: AND instead of just AND.
For parameters use #{param} to bind parameter instead of ${param} that just concatenates values to the SQL string. That does not prevent from working but that is very bad.
!= is not standard SQL and will not work for every DB vendor (although it might do with the one you are using), unlike NOT column = value,
<foreach collection="EMPLOYEE" index="index" item="item" separator=" AND ">
NOT EMP.EMP_ID = #{item}
</foreach>
furthermore better use IN:
EMP.EMP_ID NOT IN (<foreach collection="EMPLOYEE" index="index" item="item" separator=", ">
#{item}
</foreach>)

-Infinity added to database for NpgsqlParameter Timestamp field; expecting NULL

I'm inserting a record in my PostgresSQL table passing DBNull.Value for a timestamp without time zone field using a NpgsqlParameter as below:
ncmd.Parameters.Add(new NpgsqlParameter(":EndDate", NpgsqlDbType.Timestamp));
DateTime? dtval = sr.GetDateTime("EndDate");
ncmd.Parameters["EndDate"].IsNullable = true;
if (dtval.HasValue)
ncmd.Parameters["EndDate"].Value = dtval;
else
ncmd.Parameters["EndDate"].Value = DBNull.Value;
When I try to select only those records with null values for the field I use "Select * From "EVAL" Where "EndDate" IS NULL" but nothing is returned. When I read the table data in the pgAdmin I see the value "-infinity" in the field.
Please, could somebody tell me how to pass null values for a NpgsqlParameter?
Thanks in advance,
Danilo da Silva
Sorry, I've found my error. When I read the data to be inserted from a datareader, I was getting a default value for the DateTime field when it is null and so I was inserting default(DateTime) not DBNull.Value. Now I'm testing dtval.HasValue && dtval != default(DateTime) and all goes right. Thanks.