为了账号安全,请及时绑定邮箱和手机立即绑定

引起:org.hibernate.MappingException:属性映射的列数错误:

引起:org.hibernate.MappingException:属性映射的列数错误:

守着星空守着你 2021-12-10 14:42:32
我正在开发Spring Boot + Spring Data JPA + MYSQL示例。在此示例中,我在尝试运行示例时遇到以下错误。电影表有列special_features set('Trailers','Commentaries','Deleted Scenes','Behind the Scenes') DEFAULT NULL,。在 pojo 类中,我使用如下 -@Column(name="special_features")private Object specialFeatures;错误:-org.springframework.beans.factory.BeanCreationException: 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.example.model.Film.specialFeatures type: object    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1694) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:573) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE]
查看完整描述

1 回答

?
婷婷同学_

TA贡献1844条经验 获得超8个赞

Hibernate 无法处理开箱即用的 SET 数据类型。即使可以,如果将其映射到通用对象是一种方法,我也会感到惊讶。Hibernate 没有机会将其转换回字符串。

您需要做的是实现一个 AttributeConverter,它将字符串从 DB 转换为合适的数据类型,例如 EnumSet,反之亦然。看看这里的例子:Mapping EnumSet to mysql Set using JPA 2.1


查看完整回答
反对 回复 2021-12-10
  • 1 回答
  • 0 关注
  • 252 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信