How to map MySQL char (n) columns to instance variables using JPA / Hibernate annotations?
•
Java
For the "language" column in the MySQL table of type char (7), I encountered a JPA / Hibernate mapping problem In my entity, the code generated for this field is:
private String language;
This causes the following exceptions at runtime:
... 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
Thank you in advance!
J.P
Solution
Try this:
@Column(name="language",columnDeFinition="char(7)")
See if it works
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码