开发者

How to Prevent Multiple entries stored in database?

开发者 https://www.devze.com 2023-04-09 03:19 出处:网络
Consider am using java, struts, hibernate and oracle. How can i prevent duplicate entries stored in database. One way is to make field as Unique . For example i am entering country \"USA\" in jsp page

Consider am using java , struts, hibernate and oracle. How can i prevent duplicate entries stored in database. One way is to make field as Unique . For example i am entering country "USA" in jsp page,USA is already av开发者_如何学编程ailable means how can i prevent it. Please let me know.

Regards,

sara


You should always indeed put a unique constraint on fields which must stay unique. This will, however, lead to a cryptic exception at commit time. If you want to be more user-friendly, you should check if the entry already exists (using a query) before inserting it, and display a useful and readable error message to the user if the entry already exists.

This still allows two concurrent users to check at the same time, then insert at the same time, but it greatly reduces the probability, and the unique constraint makes sure that one of the commits will fail, leaving your database in a consistent state.


Query your database whether it already contains USA or not. If it does, then don't store it. If not, then do.


Add a unique index to your database table on the country column.

Additionally you can annotate the country attribute of your hibernate object with @Column(unique=true).

0

精彩评论

暂无评论...
验证码 换一张
取 消