In database collection instances, the foreign key of the object to which the collection belongs is allocated. This foreign key is called a collection key column or collection table columns.
So, I assume that you want to disable key generation for your proposal, you can just do it with this
@Entity public class Parent { @Id @GeneratedValue public Long id; @ElementCollection @CollectionTable(name = "i18n", foreignKey = @ForeignKey(name = "none"), joinColumns = @JoinColumn(name = "id")) @MapKeyColumn(name = "locale") @Column(name = "text") public Map<String, String> text = new HashMap<>(); public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Map<String, String> getText() { return text; } public void setText(Map<String, String> text) { this.text = text; } }
Then, when you check the generated DDL, the forgiveness key does not apply to table I18N, but still get the opportunity
@Test public void testParent() { Parent p = new Parent(); HashMap<String, String> text = new HashMap<>(); text.put("en", "foo"); text.put("de", "bar"); p.setText(text); entityManager.persist(p); entityManager.flush(); Parent parent = entityManager.find(Parent.class, p.getId()); System.out.println("en: " + parent.getText().get("en")); System.out.println("de: " + parent.getText().get("de")); }
About the program - a simple test (Spring Boot 1.4 release), and he will see the output in the console:
Hibernate: create table i18n ( id bigint not null, text varchar(255), locale varchar(255) not null, primary key (id, locale) ) Hibernate: create table parent ( id bigint generated by default as identity, primary key (id) ) ...... Hibernate: insert into parent (id) values (null) Hibernate: insert into i18n (id, locale, text) values (?, ?, ?) Hibernate: insert into i18n (id, locale, text) values (?, ?, ?) en: foo de: bar
This is the table in H2 db:
