org.hibernate.MappingException:属性映射在ENUM实体中具有错误的列数 [英] org.hibernate.MappingException: property mapping has wrong number of columns in ENUM entity

查看:145
本文介绍了org.hibernate.MappingException:属性映射在ENUM实体中具有错误的列数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了数据库,该数据库具有对ENUM表的引用,并且在此枚举的实体中存在异常.

I created database, which have a references with ENUM table and I have exception in entity of this 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;


因此,在创建表之后,我确实为此表自动创建了实体:


So, after creating table I did create automatically entities for this tables:

  • 资源管理器:

  • Explorer:

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

类,出现异常的地方:

  • 状态:

  • Status:

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

}

那,我在控制台中得到的东西:

That, what I get in console:

org.springframework.beans.factory.BeanCreationException:

org.springframework.beans.factory.BeanCreationException:

在类路径中创建名称为'entityManagerFactory'的bean时出错 资源[org/springframework/boot/autoconfigure/orm/jpa /HibernateJpaConfiguration.class]:调用init方法失败;嵌套>异常是javax.persistence.PersistenceException:[PersistenceUnit: 默认]无法建立Hibernate SessionFactory;嵌套的异常是 org.hibernate.MappingException:属性映射的编号错误 列:com.sb.bean.parser.explorer.model.domain2.StatusEntity.status类型:>对象

Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa /HibernateJpaConfiguration.class]: Invocation of init method failed; nested > exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: property mapping has wrong number of columns: com.sb.bean.parser.explorer.model.domain2.StatusEntity.status type: > object

我认为我确实犯了错误

  • 在创建表explorer中的字符串:

  FOREIGN KEY (status_id) REFERENCES status(code)

  • 或创建类EntityStatus

    在这种情况下,如何正确创建Status实体或在表之间创建引用?

    How to correctly create Status entity or create references between tables in this case?

    推荐答案

    Hibernate抱怨它无法映射StatusEntity.status,因为它已声明为Object类型.

    Hibernate complains that it is not able to map StatusEntity.status because it is declared Object type.

    您可以将其更改为String类型,Hibernate应该可以将其映射到数据库ENUM类型.

    You can change it to String type which Hibernate should be able to map to database ENUM type.

    您还可以将Java枚举用于status字段,并使用@Enumerated注释使它休眠.

    You can also use Java enum for status field and have hibernate map it using @Enumerated annotation.

    这篇关于org.hibernate.MappingException:属性映射在ENUM实体中具有错误的列数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆