com.mysql.jdbc.MysqlDataTruncation:数据截断:对于列' aboutMeText'在第1行

时间:2016-09-22 06:09:47

标签: spring hibernate spring-mvc jpa spring-data-jpa

我正在尝试使用Spring-JPA更新列的值,值为表情符号/表情符号。 但是收到错误说java.sql.BatchUpdateException:字符串值不正确:' \ xF0 \ x9F \ x98 \ x84 \ xF0 \ x9F ...'对于列

这是连接网址 -

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

这是调用代码

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

这是实体

 @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);


}

这是完整的错误:

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)

我尝试并检查了stackoverflow,stackexchange等上的各种帖子。并执行了一些更改,但仍然无法解决问题。

2 个答案:

答案 0 :(得分:5)

将aboutMeText的大小从1000增加到3000,代码和db通过alter。

@Column(length = 3000) 
private String aboutMeText;

这样做com.mysql.jdbc.MysqlDataTruncation:数据截断:数据太长异常消失了,我得到了所需的输出。

答案 1 :(得分:1)

您可以将此列的数据类型从varchar(255)更改为TEXT,长度为65535字节。只需将您的列标记如下:

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

或者您可以使用其他注释。例如,@Lob。访问此链接以获取详细信息https://www.baeldung.com/jpa-annotation-postgresql-text-type