263 words

Modifier operations

#1
2011-09-12 18:27

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?

 

lwalkowski
lwalkowski's picture
Joined on 2010-08-25
User Post #49
#2
2011-09-12 19:38

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
ObjectDB - Fast Object Database for Java (JPA/JDO)
support
support's picture
Joined on 2010-05-03
User Post #601
#3
2011-09-12 19:53

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
ObjectDB - Fast Object Database for Java (JPA/JDO)
support
support's picture
Joined on 2010-05-03
User Post #602
#4
2011-09-12 19:58

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

lwalkowski
lwalkowski's picture
Joined on 2010-08-25
User Post #50

Post Reply

Please read carefully the posting instructions - before posting to the ObjectDB website.

  • You may have to disable pop up blocking in order to use the toolbar (e.g. in Chrome).
  • Use ctrl + right click to open the browser context menu in the editing area (e.g. for using a browser spell checker).
  • To insert formatted lines (e.g. Java code, stack trace) - select a style in the toolbar and then insert the text in the new created block.
  • Avoid overflow of published source code examples by breaking long lines.
  • You may mark in paragraph code words (e.g. class names) with the code style (can be applied by ctrl + D).
  • Long stack traces (> 50 lines) and complex source examples (> 100 lines) should be posted as attachments.
Attachments:
Maximum file size: 32 MB
Cancel