Given this schema:
+-----------------+------------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+-------------------+-------+
| internal_id | int(10) unsigned | NO | PRI | | |
| external_id | varchar(255) | NO | PRI | | |
| mapping_type_id | int(4) unsigned | NO | PRI | | |
| creation_date | timestamp | NO | | CURRENT_TIMESTAMP | |
+-----------------+------------------+------+-----+-------------------+-------+
And this annotation:
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinTable(name="mapping",
joinColumns = { @JoinColumn(name="internal_id") },
inverseJoinColumns = { @JoinColumn(name="external_id") }
)
@WhereJoinTable(clause="mapping_type_id=2")
private List<Video> videos = Lists.newArrayList();
Which works fine with the legacy database, but when I run these annotated classes through hsqldb with a create-drop option (for basic in-memory unit tests), I get the following:
[elided]
Caused by: java.sql.SQLException: Column not found: VIDEOS0_.MAPPING_TYPE_ID in statement
[elided]
So I checked with SchemaExport, and hibernate is in fact generating the DB Schema with a join table for the mapping, but it is not including the mapping_type_id field. How do I make the schema generate this with the rest of the schema without interfering with code that runs on the existing database?
The answer to this is to create an embedded ID and use that to map the class through the table, instead of using the join-table syntax. This is easier to fake out if the join table has a row-id, but otherwise, using @Embeddable and @EmbeddedId can make it work.