Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate @MapKeyColumn and table inheritance causing Unknown column type exception

I’m upgrading from hibernate 4.2.5.Final to 4.3.6.Final, the 4.3.6 hibernate libs are causing a mysql unknown column type exception. The following classes have been simplified as I am unable to show my companies production code in its entirety.

@Entity
@Table(name = "area")
public class Area {
    private Integer id;
    private Map<BasicType, BasicConfiguration> configurationsMap =
        new HashMap<BasicType, BasicConfiguration>();

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.ALL}, orphanRemoval = true)
    @JoinTable(name = "area_configuration", joinColumns = {@JoinColumn(name = "area_id")},
               inverseJoinColumns = {@JoinColumn(name = "basic_configuration_id")})
    @MapKeyEnumerated(EnumType.STRING)
    @MapKeyColumn(name = "type")
    public Map<BasicType, BasicConfiguration> getConfigurationsMap () {
        return configurationsMap;
    }

Where BasicType is just an enum

public enum BasicType {
    TYPE1, TYPE2, TYPE3, TYPE4, TYPE5;
}

And basic configuration is:

@Entity
@Table(name = "basic_configuration")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
public abstract class BasicConfiguration {

    private Integer id;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
}

I have a test that tries to persist an area object to a mysql db which produces the following error:

**Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'type' in 'field list'**
    at sun.reflect.GeneratedConstructorAccessor73.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:408)
    at com.mysql.jd

The generated hibernate code shows that it’s trying to insert the type value into the basic_configuration table and not the area_configuration table:

**Hibernate: insert into basic_configuration (entity_version, type) values (?, TYPE1)**
Tests run: 9, Failures: 0, Errors: 9, Skipped: 0, Time elapsed: 0.208 sec <<< FAILURE! - 
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute statement
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1387)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1310)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1316)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:881)
at sun.reflect.Ge

This error seems to have been introduced in hibernate version 4.2.9.Final onwards, the versions below 4.2.9.Final do not seem to have this issue, Anyone know how I can resolve this? Many Thanks.

like image 468
user3874819 Avatar asked Jul 24 '14 22:07

user3874819


1 Answers

I remember to had a similar problem. Check here in your code:

@Entity
@Table(name = "basic_configuration")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
public abstract class BasicConfiguration {

    private Integer id;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
}

IN THIS LINE ->

@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)

You are using an SQL Reserved word, "like". Check this list:

Reserved words

like image 168
A Monad is a Monoid Avatar answered Oct 20 '22 19:10

A Monad is a Monoid