JPA: how do I persist a String into a database field, type MYSQL Text

The requirement is that the user can write an article, therefore I choose type Text for the content field inside mysql database. How can I convert Java String into MySQL Text

Here you go Jim Tough

@Entity
public class Article implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private Long userId;

    private String title;

    private String content;

    private Integer vote;

    //Constructors, setters, getters, equals and hashcode
}

In my MYSQL database, content is type Text. I was hoping that there would be something like this java.sql.Text, since java.sql.Blob is an actual type, but sadly, that does not exist

JPA and PostqreSQL: long string persistence

Can anybody tell me how to persist long text using JPA (I use PostgreSQL)? Here’s the way I defined a very long string in my class: @Lob private String body; However, this produces a field of type ch

How to persist an entity which contains a field of user type using JPA2

I’m looking for a way to persist an entity which contains a field of a user type. In this particular example I would like to persist the ts field as number of milliseconds. import org.joda.time.DateTi

How do i store a field from a MySQL database as a string in PHP?

One of my fields in my data base is an array which has been converted to a string using the implode() function. How do I retrieve the contents of this field (LESSONS) from the database and store it to

How to persist an Array List of type Entity in JPA

How to persist an Array List of type Entity in JPA ?. for example, There is an entity called Table. I am creating an array list ArrayList table= new ArrayList(); Trying to persist it using entityManag

How do I persist a list of subtypes with JPA 2.0?

I have a manager class with a list of visitable elements that I would like to persist in a database using JPA/Hibernate: @Entity class Elements { void accept(Visitor v) {…} @? List<Visitable>

How do i search from serialize field in mysql database?

How do i search from serialize field in mysql database except mysql like statement? The data is: a:9:{s:2:m1;s:4:1217;s:2:m2;s:8:9986-961;s:2:m3;s:19:1988-03-07 00:00:00;s:2:m4;s:0:;s:

How do I query UUIDs stored as binary in a database (JPA/Hibernate/MySQL)

I have a Java/JPA/Hibernate/MySQL based app. I want to use UUIDs for object identity, however I want to ensure database performance does not suffer. I found this great blog posting JPA and UUID Primar

JPA How to get the value from database after persist

How to get the value which being saved to database after entityManager.persist I am able to get the primary key value from database after calling persist not any other values. E.g. public void creat

If I change the type of a field in sqlite3 database, from string to text, will I lose the data in that field?

If I change the type of a field in my database via a Ruby on Rails migration, from string to text, will I lose the data in the field?

How do I prevent EclipseLink (JPA 2.0) from clearing my database/table?

I’m using EclipseLink (JPA 2.0) on Netbeans 7.0 with a MySQL database – which I configured according to this screencast. But sometimes when I persist an Object/Entity my table get’s cleared before tha

Answers

Since you’re using JPA, use the Lob annotation (and optionally the Column annotation). Here is what the JPA specification says about it:

9.1.19 Lob Annotation

A Lob annotation specifies that a persistent property or field should be persisted as a large object to a database-supported large object type. Portable applications should use the Lob annotation when map- ping to a database Lob type. The Lob annotation may be used in conjunction with the Basic annotation. A Lob may be either a binary or character type. The Lob type is inferred from the type of the persistent field or property, and except for string and character-based types defaults to Blob.

So declare something like this:

@Lob 
@Column(name="CONTENT", length=512)
private String content;

References

  • JPA 1.0 specification:
    • Section 9.1.19 “Lob Annotation”

With @Lob I always end up with a LONGTEXTin MySQL.

To get TEXT I declare it that ways (JPA 2.0):

@Column(columnDefinition = "TEXT")
private String text

Find this better, because I can directly choose which Text-Type the column will have in database.

For columnDefinition it is also good to read this.

for mysql ‘text’:

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

for mysql ‘longtext’:

@Lob
private String description;