Announcement Announcement Module
No announcement yet.
How to add a unique constraint to an entity and still pass integration test? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to add a unique constraint to an entity and still pass integration test?

    Hi folks,

    I have a pretty simple domain model, two entities extending a base AbstractPerson class and I'm using MySQL as my database (Using Roo 1.0.2.Release).

    enum type --class ~.reference.TriggeredAction
    enum constant --name Forward
    enum constant --name Block
    entity --class ~.domain.AbstractPerson --abstract
    entity --class ~.domain.ManagingParent --extends ~.domain.AbstractPerson --testAutomatically
    entity --class ~.domain.ManagedDependent --extends ~.domain.AbstractPerson --testAutomatically
    field string --fieldName firstName --sizeMin 3 --sizeMax 30 --class ~.domain.AbstractPerson
    field string --fieldName lastName --notNull --sizeMin 3 --sizeMax 30
    field string --fieldName telephone --notNull  --sizeMin 4
    field enum --fieldName type --type ~.reference.TriggeredAction --notNull --class ~.domain.ManagedDependent
    field reference --fieldName managingParent --type ~.domain.ManagingParent
    field set --class ~.domain.ManagingParent --fieldName managedDependents --element ~.domain.ManagedDependent --mappedBy managingParent --notNull false --cardinality ONE_TO_MANY
    When I run the generated unit tests against this setup, the tests pass successfully.

    However, when I went back and edited the base class to make the telephone field unique I started encountering issues.

    public abstract class AbstractPerson {
        @Size(min = 3, max = 30)
        private String firstName;
        @Size(min = 3, max = 30)
        private String lastName;
        @Column(unique = true)
        private String telephone;
    The tests started failing due to an issue with duplicate entries being present in the database. So I commented out the unique column constraint and re-ran the test and once again everything passed.

    So, my question is, what modifications need to be made to get these tests to pass?

    BTW, as an aside and to hopefully help someone in future, when I re-inserted the unique column annotation to the field and re-ran the tests they passed. I was very surprised to say the least. I dropped the table in the MySQL instance and re-ran the test and hey presto, it failed as expected. This occurs I had my property in persistence.xml set to update which did not change the structure of the table in which the entries were stored. If you change the property to create, then the table gets dropped and recreated with the latest structure.