Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate column annotation for TEXT

I have a spring MVC boot application with a MySQL database and I'm trying to get a TEXT field in my database. I have the following code:

Member.java

@Entity
public class Member {
private Long id;
private String name;

@Column(columnDefinition = "TEXT")
private String biography;

private String country;
private String state;
private String city;
private Date dateOfBirth;
private String gender;

//Getters and setters

application.properties

spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.datasource.url=jdbc:mysql://localhost:3306/wave
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=mysql
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.h2.console.enabled=true

And here is the Hibernate it creates

Hibernate: drop table if exists member
Hibernate: create table member (id bigint not null auto_increment, biography varchar(255), city varchar(255), country varchar(255), date_of_birth date, gender varchar(255), name varchar(255), state varchar(255), primary key (id)) ENGINE=InnoDB

It still sets it as a varchar(255). Can anyone help me with this issue? Thank you in advance.

like image 977
Martijn Jansen Avatar asked Apr 19 '17 15:04

Martijn Jansen


3 Answers

You can use as @Lob annotation, as @Column(columnDefinition="TEXT")

@Lob
private String someField;

or

@Column(columnDefinition="TEXT")
private String somefield;

Here is the Link to read more: JPA Annotation for the TEXT Type

like image 55
Victor Z Avatar answered Oct 06 '22 01:10

Victor Z


In org.hibernate.dialect.MySQLDialect class there is a line:

registerColumnType( Types.CLOB, 65535, "text" );

So based on that if you define your field like this:

@Column(length = 65535, columnDefinition = "text")
private String biography;

it should do the trick.

like image 9
Maciej Kowalski Avatar answered Nov 06 '22 16:11

Maciej Kowalski


You can use @Lob from javax.persistence... it's more elegant:

@Column
@Lob
public String getDescription() {...
like image 9
Roll Avatar answered Nov 06 '22 15:11

Roll