Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text Field using Hibernate Annotation

I am having trouble setting the type of a String, it goes like

public void setTextDesc(String textDesc) {     this.textDesc = textDesc; }  @Column(name="DESC") @Lob public String getTextDesc() {     return textDesc; } 

and it didn't work, I checked the mysql schema and it remains varchar(255), I also tried,

@Column(name="DESC", length="9000") 

or

@Column(name="DESC") @Type(type="text") 

I am trying to make the type to be TEXT, any idea would be well appreciated!

like image 592
bernardw Avatar asked Aug 15 '09 04:08

bernardw


People also ask

What is @type in hibernate?

@Type annotation is for hibernate i.e. to tell what type of data do you want to store in database. Let's take a simple example: @Type(type="yes_no") private boolean isActive; Here return type is boolean but the value which gets stored in the database will be in Y or N format instead of true / false .

What is @column in hibernate?

The @Column annotation is defined as a part of the Java Persistence API specification. It's used mainly in the DDL schema metadata generation. This means that if we let Hibernate generate the database schema automatically, it applies the not null constraint to the particular database column.


1 Answers

You said "I checked the mysql schema and it remains varchar(255)" - did you expect Hibernate to automatically alter your database? It won't. Even if you have hibernate.hbm2ddl.auto set, I don't believe Hibernate would alter the existing column definition.

If you were to generate new database creation script, @Lob should generate "TEXT" type column if you don't specify length explicitly (or if you do and it's less that 65536). You can always force that by explicitly declaring type in @Column annotation, though keep in mind that's not portable between databases:

@Column(name="DESC", columnDefinition="TEXT") 
like image 197
ChssPly76 Avatar answered Sep 23 '22 18:09

ChssPly76