Reputation: 577
Hibernate is not generating a table for the dataAttributes Map in the MetaData class below. The code compiles but table not found at runtime.
import javax.persistence.*;
import java.util.HashMap;
import java.util.Map;
@Entity
public class Metadata{
private Integer id;
private Map<String,String> dataAttributes;
public Metadata(){
dataAttributes = new HashMap<>();
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public void addDataAttribute(String key, String value){
dataAttributes.put(key,value);
}
@ElementCollection
@MapKeyColumn(name="key")
@Column(name="value")
@CollectionTable(name="data_attributes", joinColumns=@JoinColumn(name="metaData_id"))
public Map<String, String> getDataAttributes() {
return dataAttributes;
}
public void setDataAttributes(Map<String, String> dataAttributes) {
this.dataAttributes = dataAttributes;
}
}
All the other entities and tables are created as expected but this one is never generated and I get "Table 'nppcvis.data_attributes' doesn't exist" when trying to save an entity that has a one-to-one relationship with MetaData with cascade=all
I'm using the following property :
spring.jpa.hibernate.ddl-auto=create
Any ideas?
Upvotes: 9
Views: 4494
Reputation: 21
Not allowed to assign name in @MapKeyColumn as key but you can wrap in \" like here "\"key\"" In your case:
@ElementCollection
@MapKeyColumn(name="\"key\"")
@Column(name="value")
@CollectionTable(name="data_attributes", joinColumns=@JoinColumn(name="metaData_id"))
public Map<String, String> getDataAttributes() {
return dataAttributes;
}
Upvotes: 2
Reputation: 577
Removing all annotations aside from @ElementCollection result in table being created. Obviously no control over naming but it works.
Upvotes: 6