Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Longvarchar in Hibernate

This is the property,

@Column( name = "description")
private String description;

This is MySql Table enter image description here

During compiling this error is thrown,

Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [description] in table [product]; found [longtext (Types#LONGVARCHAR)], but expecting [varchar(255) (Types#VARCHAR)]

What I am missing?

like image 202
Tareq Avatar asked Feb 01 '18 15:02

Tareq


People also ask

Can hibernate map nchar data type?

Hibernate can also map nationalized character data types, like NCHAR, NVARCHAR, LONGNVARCHAR, and NCLOB. To define such a mapping, you need to annotate your entity attribute of type String with Hibernate’s @Nationalized annotation instead of @Lob .

What is a hibernate type?

A Hibernate type is a bridge between an SQL type and a Java primitive/Object type. These are the types Hibernate supports by default: You can always define your own custom types as we will see in a future article.

What are hibernate’s basic mapping rules?

It’s very important to understand the basic mapping rules for individual Entities before starting modelling Entity associations. A Hibernate type is a bridge between an SQL type and a Java primitive/Object type. These are the types Hibernate supports by default: You can always define your own custom types as we will see in a future article.

Does hibernate use lobs?

But Hibernate also needs to fetch all data stored in the LOB immediately and map it to a Java object. Depending on the size of your LOB, this can cause severe performance problems. If you, e.g., store large video files in your database, it’s often better to use JDBC’s LOB locators.


2 Answers

You can either modify the database:

ALTER TABLE product MODIFY description VARCHAR(255);

This simple ALTER TABLE command may fail if the description values are longer than 255 characters, so you may need to do it by creating a new column and transforming the values and dropping the original column.

Or you can adjust the Java code:

@Lob
@Column(name = "description", columnDefinition = "LONGTEXT")
private String description;

You can try to omit the columnDefinition first, it may be the default MySQL text LOB for Hibernate.

like image 116
Ján Halaša Avatar answered Oct 21 '22 03:10

Ján Halaša


I used @Type (org.hibernate.annotations.Type;) and it worked:

`addresses` text CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL

@Column(columnDefinition = "text")
@Type(type = "text")
private String addresses;
like image 22
Quynh Vi Avatar answered Oct 21 '22 03:10

Quynh Vi