gpt4 book ai didi

java - Hibernate @MapKeyColumn 和表继承导致未知列类型异常

转载 作者:IT老高 更新时间:2023-10-29 00:17:46 26 4
gpt4 key购买 nike

我正在从 hibernate 4.2.5.Final 升级到 4.3.6.Final,4.3.6 hibernate 库导致 mysql 未知列类型异常。由于我无法完整展示我公司的生产代码,因此简化了以下类(class)。

@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

关于java - Hibernate @MapKeyColumn 和表继承导致未知列类型异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24944783/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com