Upgrade to Hibernate4 and @ElementCollection results in inability to lookup existing data
h2, hibernate, java, sql
Solution
First question:
I don't know of any. But the JPA specification is freely available and describes what the default table and column names are.
Second question:
@ElementCollection
@CollectionTable(name = "USER_PERMISSIONS"
joinColumns = @JoinColumn(name = "USER_ID"))
@Column(name = "ELEMENT")
private List<String> permissions = Collections.emptyList();
I personally always specify the table and column names. It avoids this kind of surprise, allows me to always choose the names that I want, and is self-documentary. And I also prefer creating the database schema and DDL by myself. This ensures that all required indices, constraints, comments, etc. are created correctly.
Third question: Yes, the nature of the collection doesn't matter. See the answer to your second question to avoid these kinds of problems.
Problem
We have a model class `User` that had a `List<String>` of permissions, like so: ``` @LazyCollection(LazyCollectionOption.FALSE) @CollectionOfElements @Column() private List<String> permissions = Collections.emptyList(); ``` It produced a SQL table with an entry like the following: We recently upgraded to Hibernate 4, and as the `@CollectionOfElements` annotation has gone from deprecated to completely removed, we figured this was a good time to move over to `@ElementCollection`, like so: ``` @LazyCollection(LazyCollectionOption.FALSE) @ElementCollection // << CHANGED @Column() private List<String> permissions = Collections.emptyList(); ``` Upon launching, however, we were unable to log in with our administrative account. Curious, we examined the database using H2's JAR, and found the table had been altered: My questions: - Is there a comprehensive list of the DDL/schema changes from the old `@CollectionOfElements` to the new `@ElementCollection`? - Is there a recommended practice, or additional annotation I can provide to these properties, that will ensure backwards compatibility? (Can I specify a specific name-mapping scheme or column name?) - Finally, I assume all `Collection` types will be affected by this change - `Sets`, `Maps`, etc. I just want to ensure that we don't leave any data behind, or stone unturned, during our upgrade.