如何使用JPA / Hibernate注释将MySQL char(n)列映射到实例变量?

对于类型为char(7)的 MySQL表中的“language”列,我遇到JPA / Hibernate映射问题.在我的实体中,为该字段生成的代码是: private String language; 这在运行时会导致以下异常: … 43 moreCaused by: javax.persistence.PersistenceException: [PersistenceUn

对于类型为char(7)的
MySQL表中的“language”列,我遇到JPA / Hibernate映射问题.在我的实体中,为该字段生成的代码是:

private String language;

这在运行时会导致以下异常:

... 43 more
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: prosvetaPersistenceUnit] Unable to build EntityManagerFactory
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:911)
    at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:74)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:225)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:308)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
    ... 58 more
Caused by: org.hibernate.HibernateException: Wrong column type in joo16_dev.jos_categories for column language. Found: char,expected: varchar(255)
    at org.hibernate.mapping.Table.validateColumns(Table.java:283)
    at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1313)
    at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:139)
    at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:378)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1842)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:902)
    ... 63 more

提前致谢 !

J.P

解决方法

尝试这个:

@Column(name="language",columnDefinition="char(7)")

看看是否有效.

作者: dawei

【声明】:永州站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

为您推荐

联系我们

联系我们

0577-28828765

在线咨询: QQ交谈

邮箱: xwei067@foxmail.com

工作时间:周一至周五,9:00-17:30,节假日休息

返回顶部