MyBatis delete isn't working with multiple conditions - mybatis

I am relatively new to MyBatis. If I use only one condition like below, I don't have any issues at all.
final String DELETE = "Delete from request where author=#{author}"; // Working
final String DELETE = "Delete from request where refid=#{referenceId}"; // working
But if I give two conditions for delete, rows are not getting deleted at all.
final String DELETE = "Delete from request where refid=#{referenceId} and author=#{author}"; // Not working.
And my interface looks like this,
#Delete(DELETE)
public void delete(Request request);
Could someone help me to understand what mistake I am doing in this? I am using MyBatis 3.1.1 and MySQL
Thanks,

I don't know, after changing the return type in the interface to Integer, things started working fine.
But as per the document, http://mybatis.github.io/mybatis-3/java-api.html, return value just indicates the number of rows affected.
#Delete(DELETE)
public Integer delete(InviteRequest request); // This made stuff working with two conditions.

Related

How to read/update/delete by value(not by index) in Dart using Hive?

Sample
deleteItem(int index) {
final box = Hive.box<Delivery>("deliveries");
box.deleteAt(index);
}
I'd like to change index parameter to the id of my object), like this
deleteItem(int id) {
final box = Hive.box<Delivery>("deliveries");
// box.deleteAt(index);
// box delete by id here
}
Here my TypeAdapter class:
#HiveType(typeId: 0)
class Delivery {
#HiveField(0)
final int id;
Delivery(this.id);
}
As stated in the other answer, this is not possible unless you search for every item and compare their ID's one by one. Depending on the number of itens in your box, this may take longer or may not matter at all.
One example of this unoptimized way would be:
deleteItem(int id) {
final box = Hive.box<Delivery>("deliveries");
final Map<dynamic, Delivery> deliveriesMap = box.toMap();
dynamic desiredKey;
deliveriesMap.forEach((key, value){
if (value.id == id)
desiredKey = key;
});
box.delete(desiredKey);
}
What this code does is to turn the Box class into a Map, using the toMap() method. With a map in hands, we iterate through every entry on it, checking which one has the specific ID and keep record of that. After that we just delete the found key using the delete() method. You can read more about iterating through Maps here..
Keep in mind that this is only an example. If you try to use this you probably will need to check if there is really an item with the ID you are searching for. If it's the case, you also need to check for multiple values with the same ID in your Box.
Think of a key-value database as a regular good old vocabulary with words and their definitions. It allows you to find a definition using given word very quickly (let's say you are looking for the word elephant). However, if you wanted to find an entry with definition that says An animal with big ears., it would take you much longer time.
With a lot of simplification, that's how the key-value databases work. They are fast when you query using the index.
So if you want to query using the id, I would recommend using the id in the index itself.
For example:
Indexes:
delivery-001
delivery-002
...
Or, if you also want to perform some other more complicated queries, I would recommend using a regular SQLite database using sqflite.

#Query - Returning primitive type or String object?

I'm facing a little problem trying to retrieve an int value or String looking for Top record or last record on my DB, depending in what I want.
Looking in Spring data documentation I found something like this:
Vaccine findTopByOrderByVaccineCodeDesc();
Which is perfectly well but is returning a Vaccine, I don't want the whole Vaccine Object, I just want for example the "vaccine code". I tried something like this:
String findTop1ByOrderByVaccineCodeDesc();
But unfortunately is wrong! It's not working. But what about this:
#Query("SELECT TOP v.vaccineCode FROM Vaccine v ORDER BY vaccineCode Desc")
String getLastRecordByVaccineCode();
I know this is syntactically wrong wrote because I'm using TOP in wrong way but I don't know how to do it.
Any advice? I really appreciate a clue how to find the answer in the documentation or how to read the doc cauze I'm no good looking in the doc.
You can use EntityManager
public String getVaccineCode {
#AutoWired
EntityManager entityManager;
String vaccineCode = (String) entityManager.createQuery("SELECT v.vaccineCode FROM Vaccine v ORDER BY vaccineCode Desc", String.class).getFirstResult());
return vaccineCode;
}

How do I tell Play Framework 2 and Ebean to save null fields?

I'm using Play Framework 2 and Ebean. When a user submits a form to edit an existing object in the database, it doesn't save null values. I guess this is to prevent overwriting fields that aren't in the form with null. But how can I let them set fields in the form to null if they need to?
For example, the user edits an Event object. Event.date is 1/1/13. The user sets the Event.date field in the form to empty and submits the form. Inspecting Event.date in the debugger shows its value is null. I save the Event. If I look at the Event in the database, its value is still 1/1/13.
Edit: It seems there is a method for this. The only problem is it doesn't work on nested entities. Any solutions for this?
update(Object bean,Set<String> properties)
Create an ebean.properties file right next to the application.conf file and add this line to it:
ebean.defaultUpdateNullProperties=true
Null properties in Ebean are considered as unloaded, so to prevent accidental nulling properties that shouldn't be nulled, they are just excluded.
Because of this reverting Date (and other fields) to null in Ebean is... hard :). Last time when I had to do the same thing (revert Date) I used second query to do just... nulling the Date (after event.update(Object o)):
public static Result updateEvent(){
Form<Event> eventForm = form(Event.class).bindFromRequest();
// do some validation if required...
Event event = eventForm.get();
event.update(event.id);
if (eventForm.get().date == null){
Ebean
.createUpdate(Event.class, "UPDATE event SET date=null where id=:id")
.setParameter("id", page.id).execute();
}
}
On the other hand, if you are using comparison, for filtering events (always selecting newer than X), you can just set the date to very 'old' value, which also should do the trick. In this case you'll update the object only once.
private static final Date VERY_OLD_DATE = new GregorianCalendar(1, 0, 1).getTime();
public static Result updateEvent(){
Form<Event> eventForm = form(Event.class).bindFromRequest();
Event event = eventForm.get();
if (eventForm.get().date == null){
event.date = VERY_OLD_DATE;
}
event.update(event.id);
}
In this case in your HTML form you will need to clear the value of the form's field (or just send every time date like 0001-01-01), however it can be done easily even with JavaScript.

Sending an array as a query parameter to a rest WS (nullpointerException)

I need to call a rest web service in playframework 2 and I need to send a query parameter which is an array. In regular html I would send it like:
GET http://host.com?a=1&a=2&a=3
But when I do it when I try to do it with the playframework 2 WS api I do the next:
Map<String,String[]> paramMap = new HashMap<String, String[]>();
paramMap.put("a",new String[]{"value1","value2"});
WS.WSRequestHolder holder = WS.url("http://host.com");
Set<String> keys = paramMap.keySet();
for (int i = 0; i < paramMap.get(key).length; i++)
{
holder.setQueryParameter(key, paramMap.get(key)[i]);
}
And the first time that setQueryParamater() arrives, everything goes perfect but the second time I get a NullPointerException and paramMap.get(key)[i] is not null. Is this possible? is there any workaround?
Thanks in advance!
In regular html you need to dsend it like:
GET http://host.com?a=1&a=2&a=3
Using ampershand(&) to separate parameters in query string instead of comma(,)
Edit: Sorry forgot to type: Like ur trying to do within the loop you should not have fields with same name. You should change it as:
http://host.com?a1=value1&a2=value2
I don't know if this causes NullPointerException you face. But even if does not i suggest you change ur implementation to keep one value per field (key, value)
Ok, it seems to be a bug in play-framework, but solved in 2.1 version.
https://play.lighthouseapp.com/projects/82401/tickets/360-bug-in-wsjava-setqueryparameter-leads-to-npe-when-adding-a-query-parameter-twice

ObjectContext, Entities and loading performance

I am writing a RIA service, which is also exposed using SOAP.
One of its methods needs to read data from a very big table.
At the beginning I was doing something like:
public IQueryable<MyItem> GetMyItems()
{
return this.ObjectContext.MyItems.Where(x => x.StartDate >= start && x.EndDate <= end);
}
But then I stopped because I was worried about the performance.
As far as I understand MyItemsis fully loaded and "Where" just filters the elements that were loaded at the first access of the property MyItems. Because MyItemswill have really lots of rows, I don't think this is the right approach.
I tried to google a bit the question but no interesting results came up.
So, I was thinking I could create a new instance of the context inside the GetMyItems method and load MyItems selectively. Something like:
public IQueryable<MyItems> GetMyItems(string Username, DateTime Start, DateTime End)
{
using (MyEntities ctx = new MyEntities ())
{
var objQuery = ctx.CreateQuery<MyItems>(
"SELECT * FROM MyItems WHERE Username = #Username AND Timestamp >= #Start AND Timestamp <= #End",
new ObjectParameter("#Username", Username),
new ObjectParameter("#Start", Start),
new ObjectParameter("#End", End));
return objQuery.AsQueryable();
}
}
But I am not sure at all this is the correct way to do it.
Could you please assist me and point out the right approach to do this?
Thanks in advance,
Cheers,
Gianluca.
As far as I understand MyItemsis fully loaded and "Where" just filters the elements that were loaded at the first access of the property MyItems.
No. That's entirely wrong. Don't fix "performance problems" until you actually have them. The code you already have is likely to perform better than the code you propose replacing it with. It certainly won't behave in the way you describe. But don't take my word for it. Use the performance profiler. Use SQL Profiler. And test!