java – Hibernate @MapKeyColumn和表继承导致Unknown列类型异常

我从hibernate升级4.2.5.Final到4.3.6.Final,4.3.6 hibernate libs导致 mysql未知列类型异常.以下课程已被简化,因为我无法全面显示我的公司生产代码.
@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;
    }

BasicType只是一个枚举

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

而基本配置是:

@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;
    }
}

我有一个测试,试图持续一个区域对象到一个MysqL数据库,产生以下错误

**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

生成的hibernate代码显示它正在尝试将类型值插入到basic_configuration表中,而不是area_configuration表中:

**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

这个错误似乎已经在hibernate 4.2.9中引入了.Final起始,4.2.9以下的版本.Final似乎没有这个问题,有人知道我该如何解决这个问题?非常感谢.

解决方法

我记得有一个类似的问题.在这里查看你的代码
@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;
    }
}

在这一行 – >

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

您正在使用sql保留字,“like”.查看此列表:

Reserved words

相关文章

ArrayList简介:ArrayList 的底层是数组队列,相当于动态数组。与 Java 中的数组相比,它的容量能动态增...
一、进程与线程 进程:是代码在数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位。 线程...
本文为博客园作者所写:&#160;一寸HUI,个人博客地址:https://www.cnblogs.com/zsql/ 简单的一个类...
#############java面向对象详解#############1、面向对象基本概念2、类与对象3、类和对象的定义格式4、...
一、什么是异常? 异常就是有异于常态,和正常情况不一样,有错误出错。在java中,阻止当前方法或作用域...
Collection接口 Collection接口 Collection接口 Collection是最基本的集合接口,一个Collection代表一组...