I'm new to Hibernate and maybe I'm off on a wrong track. Please set me straight.
The behavior I want in my application is I create a number of things and stick 'em in a database using Hibernate. If they're already there, skip the insert.
A field in the database table is "uniqueIdentifier" and I've set it to be "unique". In my Hibernate file, I have:
<property name="uniqueIdentifier" unique="true" />
My assumption was that Hibernate would deal with the issue if I attempt to insert a new record that already has been entered. Instead, I'm getting either an SQLException or a Hibernate exception when it tries to insert a record that's already in the database.
Am I misunderstanding the purpose of "unique"? Is this a way to keep duplicate non-primary-key values from getting into the database? If not, what's the 'hibernate way' of doing a series of inserts such that no two records in the database will end up with the same value in a particular field without throwing an exception (which seems to make the Session upset)?
Thanks in advance. hbm and sql statements are below.
Dave
=====================
<hibernate-mapping>
<class name="newsprint.MyNewsArticle" table="ARTICLE">
<id name="articleId" type="int" column="articleId" unsaved-value="0">
<generator class="identity"/>
</id>
<property name="rawHtml"/>
<property name="cleanText"/>
<property name="publicationId"/>
<property name="pubDate"/>
<property name="headline"/>
<property name="uniqueIdentifier" unique="true" />
</class>
</hibernate-mapping>
=====================
CREATE table article (
articleId int(11) PRIMARY KEY NOT NULL auto_increment,
publicationId int(11),
headline varchar(255),
rawHtml text,
cleanText text,
uniqueIdentifier varchar(255) unique,
pubDate date
);
|