If you have access to database then I'd suggest creating a new column hash.
For example Please follow along with this script
drop database if exists test_md5;
create database test_md5;
use test_md5;
CREATE TABLE if not exists `test_users` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`username` varchar(255) not null,
PRIMARY KEY (`id`)
);
CREATE TABLE if not exists `test_posts` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`title` text NOT NULL,
`user_id` bigint(20) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_post_user` (`user_id`),
CONSTRAINT `fk_post_user` FOREIGN KEY (`user_id`) REFERENCES `test_users` (`id`)
);
-- Users
insert into test_users(username) values('john');
insert into test_users(username) values('jane');
-- John's test_posts Content
insert into test_posts(title, user_id) values('hey i am john, this is my very first post', 1);
insert into test_posts(title, user_id) values('hey i am john, this is my very first post', 1);
insert into test_posts(title, user_id) values('hey i am john again, i\'ll be going off from website for couple of days', 1);
-- Jane's test_posts Content
insert into test_posts(title, user_id) values('hey i am jane, this website looks a biut dry', 2);
Please note that my second row has same content as row 1.
Now when query them I add extract colum for the fields which makes up the data integrity of either single row item or from multiple table.
When we add posts.id column
to concat
function it makes the rows distinguishable.
SELECT posts.id, posts.title, users.username,
md5(concat(posts.title, users.username))
as hash from test_posts as posts
inner join test_users as users where posts.user_id = users.id
In the first iteration, you would return hash
along with data.
In the second iteration, you would have the hash in request and hash from the sql response or from db layers, you can compare these 2 values and decide further what else needs to be done.
Another approach, is that you implement meaningful equals and hashcode
for you every model representing in the database.
Another solution would be to convert string to JSON and compute checksum and compare, but that is more inefficient since it involves adding extras json library and conversion to JSON is costly magic.
Reference from here:
https://ckhconsulting.com/row-or-rows-md5-hash-with-mysql/