Increment column value automatically in every update - spring

I have created Entity having field as updateCount (non id column),
While first save.. I provide value = 1 for this column. But for every time if I update this entity , I want this column should auto increment.
Is there any way in Spring Jpa/Hibernate to do so? This will help to get latest update count for that record in case of concurrent update.

use #Version int version in your model; spring does it for you automaitcally in every update

Related

Spring Jpa- Instead of updating a row, I'm accidentally adding a new one

Spring Jpa- Instead of updating a row, I'm accidentally adding a new one with different id(primary key). I'm using .save() to update a row, but instead of updating its adding new row in the table..
Here, instead of "where task_user_id" I need task_tId, which is the primary key.. How do I do that??
Log : select task0_.t_id as t_id1_0_, task0_.creation_date as creation2_0_, task0_.status as status3_0_, task0_.target_date as target_d4_0_, task0_.task_desc as task_des5_0_, task0_.user_id as user_id6_0_ from tasks task0_ where task0_.user_id=? limit ?

postgresql custom primarykey

I try to make a project using hibernate and postgres as DB. The problem I have is I need to store the primary key as this 22/2017 or like 432/1990.
Let's say the first number is object_id and second year_added.
I think what I want to achieve is to make a first number and second number together a primary key so 22/2017 is different from 22/2016.
The only idea I have is when user add new object I generate current date year and trying to find last id and increment it.
So next year first added object should be : 1/2018.
So far in my db only object_id is stored as a primary key.
This solution seems to work fine:
PostgreSQL: Auto-increment based on multi-column unique constraint
Thanks for helping me anyway.

How to get value from a specific row and column in MS ACCESS database?

First, I don't have unique value to directly point out a value I want to retrieve, so there for, I cannot use the WHERE Statement. The reason for that, because I have a database where I constantly updated or deleted, so if I use WHERE statement I have to edit the code again.
Then do apply a unique value - add a field of data type AutoNumber or you will never get out of this mess.

date difference in terms of days using mongotemplate

I have 3 columns in my mongodb named as days (long), startDate (java.util.Date), endDate (java.util.Date). What all I want to fetch the records between startDate and (endDate-days) OR (endDate-startDate) <= days.
Can you please let me know how could i achieve this using mongoTemplate spring.
I don't want to fetch all the records from table and then resolve this on java side since in future my table may have million of records.
Thanks
Jitender
There is no way to do this in the query on the DB side (the end minus start part). What I recommend if this is an important feature for your application is that you alter the schema to maintain in the document the delta between the two fields in the format you need it. Since you can update that field when you update endDate (or if you populate both dates at the same time you can just compute the field then).
If you receive this data in bulk from another source, or if you do multi-updates of the endDate then you will probably need another job to run and periodically compute the delta of the documents where it's not computed (then you can start with always setting delta to 99999 and update it in this job to accurate value once endDate is set).
While you can use $where clause, it will be a very slow full collection scan so I would not suggest its use - it's probably better to come up with a more performant alternative even if it requires altering the schema.
http://docs.mongodb.org/manual/reference/operator/where/

making a global variable and incrementing in function - CodeIgniter

I'm trying to make a global variable in my model to increment an id field in my mysql table, but CI won't allow me to make global variables.
I've searched around and people are mentioning having to create a library and declaring the global variable there, and then loading the library in the model and using the variable.
I understand that approach, but how/where would I be able to increment the variables value so that the next time it's called in a function it will be +1?
Thank you in advance for any help
edit: the thing I'm trying to do is whenever I insert a new row in a table, I want to continue on from the last entries id number i.e. last id number in the table is 9, I want to increment on from that number and make the new rows id number 10
If you're able to access the database through CI you can use the Active Record count_all() function.
From CI docs http://ellislab.com/codeigniter/user-guide/database/active_record.html
Permits you to determine the number of rows in a particular table.
Submit the table name in the first parameter. Example:
echo $this->db->count_all('my_table');
// Produces an integer, like 25
You would just get the number of rows in the table returned and add 1 for the row in your new entry.

Resources