Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'aboutMeText' at row 1

I am trying to update value of a column using Spring-JPA, values are emoji/smilies. but getting error saying java.sql.BatchUpdateException: Incorrect string value: '\xF0\x9F\x98\x84\xF0\x9F...' for column

Here is the connection URL-

jdbc.url=jdbc:mysql:localhost:3306/woo?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&connectionCollation=utf8mb4_unicode_ci&characterSetResults=UTF-8

Here is the calling code

userProfile.setAboutMeText("\uD83D\uDE04\uD83D\uDC68\u200D\u2764\uFE0F\u200D\uD83D\uDC8B\u200D\uD83D\uDC68\uD83D\uDE02\uD83D\uDE20");

Here is the entity

 @Entity
public class UserProfile implements Serializable {

    @Column(length = 1000)
private String aboutMeText;
@Id
private Long id;
public Long getId() {
    return id;
}

public void seId(Long id) {
    this.id = id;
}


public String getAboutMeText() {
   return JsonEscape.unescapeJson(aboutMeText);


}
 public void setAboutMeText(String aboutMeText) {
   this.aboutMeText = JsonEscape.escapeJson(aboutMeText);


}

and

here is the complete error:

HTTP Status 500 - Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: Data truncation: Data too long for column 'aboutMeText' at row 1; SQL [n/a]; nested exception is org.hibernate.exception.DataException: Data truncation: Data too long for column 'aboutMeText' at row 1</h1>
    <div class="line"></div>
    <p>
        <b>type</b> Exception report
    </p>

<pre>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: Data truncation: Data too long for column 'aboutMeText' at row 1; SQL [n/a]; nested exception is org.hibernate.exception.DataException: Data truncation: Data too long for column 'aboutMeText' at row 1
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:981)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:871)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

I tried and checked various posts on stackoverflow , stackexchange etc.. and performed some changes, but still unable to solve the issue.

like image 378
Asad Ali Avatar asked Feb 07 '23 02:02

Asad Ali


2 Answers

increased size of aboutMeText from 1000 to 3000, in code and db through alter.

@Column(length = 3000) 
private String aboutMeText;

doing so com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long exception Gone and i got desired output.

like image 171
Asad Ali Avatar answered Feb 08 '23 16:02

Asad Ali


You can change the data type of this column from varchar(255) to TEXT with length of 65535 bytes. Just mark your column as below:

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

Or you can use other annotations. For instance, @Lob. Visit this link for details https://www.baeldung.com/jpa-annotation-postgresql-text-type

like image 28
Mykola Shorobura Avatar answered Feb 08 '23 14:02

Mykola Shorobura