Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate not creating ElementCollection table

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?

like image 474
Sion Griffiths Avatar asked Mar 13 '16 13:03

Sion Griffiths


1 Answers

Removing all annotations aside from @ElementCollection result in table being created. Obviously no control over naming but it works.

like image 56
Sion Griffiths Avatar answered Nov 03 '22 10:11

Sion Griffiths