Hibernate is incorrectly generating a unique index on and emdeddable collection - the uniqueness of the index is stopping certain rows being added to the collection.The test case is very simple.
Define an embeddable. @Embeddable public class Address { private int houseNumber = 0; private String streetName; }
Then define an Entity @Entity public class AddressBook { @Id @GeneratedValue private long id; @ElementCollection @CollectionTable(name = "ADDRESS_BOOK_SET") private Set<Address> addresses = new HashSet<Address>(); }
Then is a test case add 2 Addresses to the address book with the same house number but a different street. @Test public void test99() { TransactionStatus tstat = txManager.getTransaction(null); AddressBook e = new AddressBook(); Address address1 = new Address(12, "High Street"); Address address2 = new Address(12, "Park Avenue"); e.getAddresses().add(address1); e.getAddresses().add(address2); dao.save(e); txManager.commit(tstat); }
This will throw a DataIntegrityException on commit as the index on join table is on the columns (ADDRESSBOOK_ID, HOUSENUMBER) BUT it is a unique index meaning you can have 2 addresses in your address book with the same house number even if they are on different streets!
Test case attached.
|