Hi,
I am new to hibernate and I am migrating a custom mysql-application to hibernate.
I have a Table called SHA1Table (which is used for storing vast amounts of SHA1 hashes). The custom SQL statement for creating this table is:
CREATE TABLE SHA1Table ( SHA1ID integer AUTO_INCREMENT, SHA1VAL VARCHAR (40) not null, PRIMARY KEY (SHA1ID), UNIQUE INDEX (SHA1VAL(40)))
Using Hibernate I created the class
<class name="events.SHA1Hash" table="SHA1Table">
<id name="id" column="HASH_ID">
<generator class="native"/>
</id>
<property name="hashvalue" unique="true"/>
</class>
As the hashvalue is unique, I'd like to insert a hashvalue only if it's different from the existing ones.
SHA1Hash theHash = new SHA1Hash();
theHash.setHashvalue(hashvalue);
session.save(theHash);
This clearly fails if an entry is saved a second time, because the unique constraint is violated.
Is the correct way to insert the newly created "theHash" into the database by first querying whether the value "hashvalue" is already present in SHA1Hash and then to decide upon the result whether to insert or not? If yes this is a problem as I have to insert at least 46000000 entries.
It would be a nice feature to have the database do this and just return a SHA1ID (i.e. to have the database ensure that there is a certain hash entry stored and assigned an ID).
Jasper
|