我从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”.查看此列表: