Reputation: 7076
I don't know this is already asked or not. But I don't find anyone. (Anyway this is not required for my project. But when I was working with database and Hibernate I had a doubt and now got time to ask here. So I have not tried so far about what I am going to ask here)
Suppose I have table in my database. I want to insert data into the table, but only Unique rows.
How it can be possible?
From, this SO Qestion, i think, we can insert unique rows into databse.
So in Java, Using PreparedStatement
, can we be able to pass the same query so that it will insert rows only if that row is unique in the table?
If it is possible, what ERROR/EXCEPTION, Java will throw?
If not possible, what will be the best way to do so?
And also how about if we are using hibernate? How can we do the same?
If I am passing the bean class object to save the fields, how can I check for Unique rows?
Also how to perform the same with List of Objects?
Hope everyone Understood the situation.
Upvotes: 0
Views: 1754
Reputation: 3368
So in Java, Using PreparedStatement , can we be able to pass the same query so that it will insert rows only if that row is unique in the table?
If you have set unique constraints for your fields in your table, it will fail inserting duplicates.
If it is possible, what ERROR/EXCEPTION, Java will throw?
Your database will raise an error; something like :
Duplicate entry 'xxxx' for key 'whatever'
Hopefully your java application will bring up that error from the database in a way or another...
If I am passing the bean class object to save the fields, how can I check for Unique rows?
Also how to perform the same with List of Objects?
If you are manipulating beans then simply override equals()
and use a java.util.Set
instead of a List
when inserting multiple rows.
Anyway, like @Muel said: "It's much better to enforce uniqueness at database level not application level." so as to never get corrupted data in your database. However, it is also very easy to override equals
in your beans and check if you don't have two (or more) objects which are identical.
Upvotes: 1
Reputation: 67
Hibernate works on object identifier (id), if object already have identifier then hibernate will try to update that object instead of insertion.
You can use simple solution like : get object on primary id from db , if object exist extract identifier and set identifier to new object it will be updated , if object does not exist it will be inserted and use "saveOrupdate"
From hibernate docs :
Hibernate users have requested a general purpose method that either saves a transient instance by generating a new identifier or updates/reattaches the detached instances associated with its current identifier
saveOrUpdate() does the following:
if the object is already persistent in this session, do nothing
if another object associated with the session has the same identifier, throw an exception
if the object has no identifier property, save() it
if the object's identifier has the value assigned to a newly instantiated object, save() it
if the object is versioned by a or , and the version property value is the same value assigned to a newly instantiated object, save() it otherwise update() the object
Upvotes: 1