We have a table which refers to prospective customers (called Prospect). Each prospective customer is in only one Campaign (many-to-one). Each prospective customer also has two keys, purl_key and external_key.
These keys are
both unique within a Campaign. i.e. we would like the SQL to be
Code:
create table Prospect (
id BIGINT NOT NULL AUTO_INCREMENT,
campaign BIGINT,
purl_key VARCHAR(50),
external_key VARCHAR(50),
primary key (id),
unique (campaign, purl_key),
unique (campaign, external_key)
);
However, we can't see how to use the unique-key attribute twice on the campaign column -- it seems that you can only have one unique-key attribute on a single column. Is this true?
Here are the details:
Hibernate version: 2.1. (Is there a version number in the hibernate2.jar anywhere? If so, where? If not, can one be added?)
Mapping documents:Code:
<hibernate-mapping>
<class name="com.nimblefish.core.domain.prospect.Prospect">
<id name="id" type="long" unsaved-value="null" >
<meta attribute="scope-set">protected</meta>
<generator class="native"/>
</id>
<many-to-one name="campaign" class="com.nimblefish.core.domain.campaign.Campaign">
<column name="campaign" unique-key="campaignPurl_key"/>
</many-to-one>
<!-- purl keys must be unique within a campaign! -->
<property name="purl_key" type="string">
<column name="purl_key" length="50" unique-key="campaignPurl_key"/>
</property>
<!-- external_key must also be unique within a campaign! -->
<property name="external_key" type="string">
<column name="external_key" length="50"/> <!-- also want a unique-with-campaign constraint here, but how??? -->
</property>
</class>
</hibernate-mapping>
Name and version of the database you are using:MySQL 4.0.17
The mapping above sets up only the unique constraint on (campaign, purl_key). That works fine. But we can't set up another unique constraint on (campaign, external_key).
We have tried the following mapping variations:
Code:
<many-to-one name="campaign" class="com.nimblefish.core.domain.campaign.Campaign">
<column name="campaign" unique-key="campaignPurl_key"/>
<column name="campaign" unique-key="campaignExternal_key"/>
</many-to-one>
<!-- purl keys must be unique within a campaign! -->
<property name="purl_key" type="string">
<column name="purl_key" length="50" unique-key="campaignPurl_key"/>
</property>
<!-- external_key must also be unique within a campaign! -->
<property name="external_key" type="string">
<column name="external_key" length="50" unique-key="campaignExternal_key"/>
</property>
Code:
<many-to-one name="campaign" class="com.nimblefish.core.domain.campaign.Campaign">
<column name="campaign" unique-key="campaignPurl_key,campaignExternal_key"/>
</many-to-one>
<!-- purl keys must be unique within a campaign! -->
<property name="purl_key" type="string">
<column name="purl_key" length="50" unique-key="campaignPurl_key"/>
</property>
<!-- external_key must also be unique within a campaign! -->
<property name="external_key" type="string">
<column name="external_key" length="50" unique-key="campaignExternal_key"/>
</property>
Neither works.
Is there any way to do this with the 2.1 release (or any other release) of hbm2ddl? And if so, what should the mapping look like?
Thanks very much!
Cheers,
Rob