Question

As given in the below code I am inserting data in table by simple hibernate code. But when all the fields are then also it is saving data in table but only id is changed (auto-incremented). I want to know is there any way to know that the same data exists in table & I can save redundant inserts. Please tell an easy way I know I can write queries anytime to figure out the same.

List<Route> listRoute = newList();
listRoute.get(0).setSource("DelhiTest");
listRoute.get(0).setDestination("KotaTest");
listRoute.get(1).setSource("DelhiTest");
listRoute.get(1).setDestination("KotaTest");
listRoute.get(2).setSource("DelhiTest");
listRoute.get(2).setDestination("KotaTest");
RowReferenceByEntity.setListRoute(listRoute);
new RouteDAOImpl().saveOnly(listRoute.get(0));
new RouteDAOImpl().saveOnly(listRoute.get(1));
new RouteDAOImpl().saveOnly(listRoute.get(2));

Thanks

Was it helpful?

Solution

Searching for duplicate rows already persisted can lead to performance issues. For each new item you ill need to search a match in that table. To guarantee routes are unique you can create a unique index covering source and destination ids. Any attempt to duplicate data ill throw an exception. and this ill work fast.

But if all you want is to just find duplicate items in a list look at this post. How to find duplicate items in list

OTHER TIPS

This query counts how many ids have the same value:

SELECT COUNT(id), value
FROM table
GROUP BY value
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top