2016-09-22 238 views
2

我想使用Spring-JPA更新列的值,值是表情符号/表情符号。 但得到错误说java.sql.BatchUpdateException:不正确的字符串值: '\ XF0 \ x9F \ X98 \ X84 \ XF0 \ x9F ......' 对于com.mysql.jdbc.MysqlDataTruncation:数据截断:数据太长的列'aboutMeText'在第1行

这里是连接URL-

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) 

我尝试和检查了计算器,stackexchange等各个岗位..和执行了一些变化,但仍无法解决问题。

+0

将排序规则更改为utf8_unicode_ci ... [检查此](http://stackoverflow.com/a/20431999/4117061)和[this](http://stackoverflow.com/a/10959780/4117061) –

+0

previous它是utf8_unicode_ci,但我得到相同的错误,所以将其改为utf8mb4_unicode_ci –

+0

它是一个二进制数据,你正在存储在该列?那么它不应该是字节[]而不是字符串blob类型? –

回答

3

将aboutMeText的大小从1000增加到3000,在代码中和db通过改变。

@Column(length = 3000) 
private String aboutMeText; 

这样com.mysql.jdbc.MysqlDataTruncation:数据截断:数据太长例外过去了,我得到了所需的输出。

+0

你的数据库的列定义呢? –

+0

在db中增加太多了......'alter table UserProfile modify aboutMeText varchar(3000);'现在这样做我现在可以发送至少250个emojis,我只能发送85个emojies,如果我输入了超过85个emojies,数据太长例外。这意味着每个表情符号都被编码并因此增加了尺寸。 –

相关问题