Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map a MySQL char(n) column to an instance variable using a JPA/Hibernate annotation?

I encounter a JPA/Hibernate mapping problem on a column "language" in a MySQL table whose type is char(7). In my entity, the code generated for the field is:

    private String language;

this causes the following exception 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

Thanks in advance !

JP

like image 812
Jean-Pierre Schnyder Avatar asked Jun 01 '11 21:06

Jean-Pierre Schnyder


People also ask

Which JPA annotation can be used to map the class field to a DB column?

The @Basic annotation is used to map a basic attribute type to a database column.

Which annotation in JPA is used to customize the mapping of a field?

@Column. Let's start with the @Column annotation. It is an optional annotation that enables you to customize the mapping between the entity attribute and the database column.

What is @basic annotation in JPA?

We can use the @Basic annotation to mark a basic type property: @Entity public class Course { @Basic @Id private int id; @Basic private String name; ... } In other words, the @Basic annotation on a field or a property signifies that it's a basic type and Hibernate should use the standard mapping for its persistence.


2 Answers

Try this:

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

see if that works.

like image 74
Femi Avatar answered Sep 28 '22 04:09

Femi


For me*Grails it's works: sqlType: "char" on mapping

like image 45
lucasddaniel Avatar answered Sep 28 '22 05:09

lucasddaniel