I'm using JUnit to specify and run my out-of-container (unit and) integration tests. When kicking of my testcases I let hbm2ddl generate the db schema ("create" option) from my mapping files. So far, so good, everything works fine. It's a good procedure for out-of-container JUnit (unit and) integration testing: hibernate produces the schema in the db, so I don't have to worry writing sql (yet).
But now I have the following situation:
I've got a User.hbm.xml file containing a bunch of properties and the following assocation mapping:
<set name="emailAddresses" cascade="all,delete-orphan">
<key column="USER_SK" not-null="true"/>
<one-to-many class="EmailAdress"/>
</set>
I've also got the EmailAdress.hbm.xml file. It also contains a bunch of properties, such as the emailUrl property. It doesn't however have a reference back to User because there's no need for me to navigate from EmailAdress to User. So in fact I guess I have a unidirectional one-to-many association from User to EmailAddress ?
In Java I've got the corresponding User and an EmailAdress classes. In my User Java class I've defined a private Set emailAddresses field with an appropriate getter and setter.
I've also added an addEmailAdress(EmailAdress emailAdress) method, to add an EmailAdress to the set of emailadresses.
In the EmailAdress class I don't have a field pointing towards User, again because I don't have the need for it. I only have a bunch of fields, one of them being the emailUrl field.
The problem I'm facing is that I can't use hbm2ddl anymore when kicking-off my JUnit testcases, because I'm unable to let it generate all the unique-key/index constraints that I need. Because it can't generate all the required constraints anymore my tests that assume the presence of these constraints fail.
More specifically what I would like is for hbm2ddl to generate a unique key constraint on the EmailAdress table and more specifically on the combination of its EMAIL_URL+USER_SK (the foreign key) columns.
I need this constraint because although a User can have many EmailAddresses each should be unique for that user. In other words the EMAIL_URL+USER_SK combination should be unique.
However it seems there's no way to specify the above kind of combined unique constraint because the EmailAdress hbm doesn't have a property that maps to User.
So the question is whether and how I can specify the above constraint in anyway in my mapping files so that hbm2ddl can fully generate it ?
If it's not possible, I guess I could still let hbm2ddl generate the ddl and create the schema and then invoke a postprocessing sql script that would alter the schema created by hbm2ddl (by letting it add the user_SK+emailUrl unique constraint on the adress table) ?
Of course, this is a cumbersome approach. It would be better if hbm2ddl could generate all the constraints needed.
What are my options ? Any help would be appreciated.
Thanks,
EDH
|