I have an entity that embeds an object which contains a map.
If I change the column name, the map key of the embedded object disappears in the schema generated by the hbm2ddl tool.
Example:
The Product entity
Code:
package com.demo.entity;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Product {
private Long id;
private LocalString description;
@Id @GeneratedValue
public Long getId() {
return id;
}
@Embedded
public LocalString getDescription() {
return description;
}
}
The LocalString component
Code:
package com.demo.entity;
import java.util.Locale;
import java.util.Map;
import javax.persistence.ElementCollection;
import javax.persistence.Embeddable;
@Embeddable
public class LocalString {
private Map<Locale, String> localStrings;
@ElementCollection
public Map<Locale, String> getLocalStrings() {
return localStrings;
}
}
The hibernate configuration
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.datasource">java:/comp/env/jdbc/dbprod</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="id.new_generator_mappings">true</property>
<mapping class="com.demo.entity.Product" />
</session-factory>
</hibernate-configuration>
The generated schema (good)
Code:
create table Product (id bigint not null, primary key (id));
create table Product_localStrings (Product_id bigint not null, localStrings varchar(255), localStrings_KEY varchar(255), primary key (Product_id, localStrings_KEY));
alter table Product_localStrings add index FKA32FCA07406721A1 (Product_id), add constraint FKA32FCA07406721A1 foreign key (Product_id) references Product (id);
create table hibernate_sequence ( next_val bigint );
insert into hibernate_sequence values ( 1 );
Now I change the column name:
Code:
@Embedded
@AttributeOverrides( {
@AttributeOverride(name="localStrings", column = @Column(name="desc") )
})
public LocalString getDescription() {
return description;
}
The generated schema becomes:
Code:
create table Product (id bigint not null, primary key (id));
create table Product_localStrings (Product_id bigint not null, desc varchar(255), primary key (Product_id, desc));
alter table Product_localStrings add index FKA32FCA07406721A1 (Product_id), add constraint FKA32FCA07406721A1 foreign key (Product_id) references Product (id);
where localStrings_KEY has disappeared.
How to fix this?
Thank you.