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

我正在开发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;

http://img2.mukewang.com/61b2f6ed00012d9e11940276.jpg

错误:-


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]


守着星空守着你
浏览 253回答 1
1回答

婷婷同学_

Hibernate 无法处理开箱即用的 SET 数据类型。即使可以,如果将其映射到通用对象是一种方法,我也会感到惊讶。Hibernate 没有机会将其转换回字符串。您需要做的是实现一个 AttributeConverter,它将字符串从 DB 转换为合适的数据类型,例如 EnumSet,反之亦然。看看这里的例子:Mapping EnumSet to mysql Set using JPA 2.1
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java