org.hibernate.mappingexception:属性Map在枚举实体中的列数错误

9ceoxa92  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(242)

我创建了一个数据库,它有一个带有enum表的引用,我在这个enum的实体中有一个异常。
脚本示例:

CREATE TABLE status (
  code VARCHAR(40),
  status ENUM('not started', 'in progress', 'finished')
);

insert into status (code, status)
values (1, 'not started'),
       (2, 'in progress'),
       (3, 'finished');
CREATE TABLE `explorer` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `create_date` datetime DEFAULT NULL,
  `query` varchar(255) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL,
  `title` varchar(255) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL,
  `status_id` int DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (status_id) REFERENCES status(code)
) ENGINE=MyISAM AUTO_INCREMENT=45 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;

因此,在创建表之后,我确实为这些表自动创建了实体:
资源管理器:

@Entity
@Table(name = "explorer", schema = "parsebeandeveloper", catalog = "")
public class ExplorerEntity {
private long id;
private Timestamp createDate;
private String query;
private String title;
private Integer statusId;

@Id
@Column(name = "id")
public long getId() {
    return id;
}

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

@Basic
@Column(name = "create_date")
public Timestamp getCreateDate() {
    return createDate;
}

public void setCreateDate(Timestamp createDate) {
    this.createDate = createDate;
}

@Basic
@Column(name = "query")
public String getQuery() {
    return query;
}

public void setQuery(String query) {
    this.query = query;
}

@Basic
@Column(name = "title")
public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

@Basic
@Column(name = "status_id")
public Integer getStatusId() {
    return statusId;
}

public void setStatusId(Integer statusId) {
    this.statusId = statusId;
}
}

类,我得到异常:
状态:

@Entity
@Table(name = "status", schema = "parsebeandeveloper")
public class StatusEntity {
private Integer code;
private Object status;

@Basic
@Column(name = "code")
public Integer getCode() {
    return code;
}

public void setCode(Integer code) {
    this.code = code;
}

@Basic
@Column(name = "status")
public Object getStatus() {
    return status;
}

public void setStatus(Object status) {
    this.status = status;
}

}
我在控制台里看到的是:
org.springframework.beans.factory.beancreationexception:异常:
创建在类路径资源[org/springframework/boot/autoconfigure/orm/jpa/hibernatejpaconfiguration.class]中定义的名为“entitymanagerfactory”的bean时出错:调用init方法失败;nested>exception为javax.persistence.persistenceexception:[persistenceunit:default]无法构建hibernate sessionfactory;嵌套异常为org.hibernate.mappingexception:属性Map的列数错误:com.sb.bean.parser.explorer.model.domain2.statusentity.status type:>object
我想,我确实犯了错误
在创建表中 explorer ,在字符串中:

FOREIGN KEY (status_id) REFERENCES status(code)

或者在创建类中 EntityStatus 如何正确创建 Status 在这种情况下,是实体还是在表之间创建引用?

olhwl3o2

olhwl3o21#

hibernate抱怨它无法Map StatusEntity.status 因为它被宣布 Object 类型。
你可以把它改成 String hibernate应该能够Map到数据库枚举类型的类型。
您还可以使用java枚举 status 字段并让hibernate使用 @Enumerated 注解。

相关问题