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.
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
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.
You can use @Lob
from javax.persistence... it's more elegant:
@Column
@Lob
public String getDescription() {...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With