Modifier operations

#1

Hi,

Today I was reading about different NoSQL databases. One of those database - MongoDB - has a very nice feature, especially for web applications - modifier operations.

The whole trick for those operations is to change some value in object without pulling this object from database. So for example we have class:

public class WebVisit {
    public String url; // key
    public int visitsCount;
}

As for now I have to pull this class from database:

WebVisit visit = em.find(WebVisit.class, url);

and increment counter:

visit.visitsCount++;

So, what I propose is a method that will take class and field name and increment value in this field:

em.incValue(WebVisit.class, "visitsCount");

The same for decValue(). Maybe some method for adding element to the list:

em.addList(ClassWithList.class, "field_list", value);

What do you guys think about it?

 

#2

You can use a JPQL update query to increase a field value without fetching it:

    Query query = em.createQuery(
      "UPDATE WebVisit v SET v.visitsCount = v.visitsCount + 1");
    query.executeUpdate();

Update queries are supported by ObjectDB since version 2.2.4 but undocumented yet (the manual will be updated soon for version 2.3).

This has the advantage of being a portable JPA code but it is limited to simple value field modification. Adding an item to a list will require an ObjectDB extension.

ObjectDB Support
#3

If the list is managed as an inverse (mapped by) field - you should be able to use an update query on the owner side, setting the owner side field to a parameter. If you try it - please report if it works because it has not been tested yet.

ObjectDB Support
#4

You are right. I totally forget about update statement for such simple example :)

Reply