我创建了数据库,该数据库具有对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:
在类路径中创建名称为'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类型:>对象
我认为我做错了
在创建表explorer
中,字符串:
FOREIGN KEY (status_id) REFERENCES status(code)
或创建类EntityStatus
在这种情况下,如何正确创建Status
实体或在表之间创建引用?
答案 0 :(得分:1)
Hibernate抱怨它无法映射StatusEntity.status
,因为它被声明为Object
类型。
您可以将其更改为String
类型,Hibernate应该可以将其映射到数据库ENUM类型。
您还可以将Java枚举用于status
字段,并使用@Enumerated
注释使其休眠。