spring mvc特殊字符

时间:2018-05-22 16:59:24

标签: regex spring hibernate spring-mvc spring-boot

我正在尝试添加一个包含特殊字符的字段,例如“Patrícia”或“José”,因为该字段是名称,我使用的是正则表达式模式

^[a-zA-Z\u00C0-\u00FF]+ 

https://regexr.com/

中测试

这是用户模型中的字段:

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "user_id")
private Integer id;

(etc)

@Column(name = "name")
@Pattern(regexp = " ^[a-zA-Z\u00C0-\u00FF]+" , message = "{user.name.pattern}")
@NotEmpty(message = "{user.name.empty}")
@Size(min=1, max=40, message = " {user.name.size}")
private String name;

(getters/setters/constructor)

如果我添加Patricia或Jose,例如验证成功并插入用户。使用这些名称在regExr中正则表达式似乎没问题......

这是我得到的错误:

Hibernate: insert into user (f1, f2, f3, last_name, name, f6, f7, user_id) values (?, ?, ?, ?, ?, ?, ?, ?)
2018-05-22 17:40:34.622  WARN 8475 --- [nio-8080-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1062, SQLState: 23000
2018-05-22 17:40:34.623 ERROR 8475 --- [nio-8080-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : Duplicate entry '3' for key 'PRIMARY'
2018-05-22 17:40:34.624  INFO 8475 --- [nio-8080-exec-3] o.h.e.j.b.internal.AbstractBatchImpl     : HHH000010: On release of batch it still contained JDBC statements
2018-05-22 17:40:34.626 ERROR 8475 --- [nio-8080-exec-3] o.h.i.ExceptionMapperStandardImpl        : HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]
2018-05-22 17:40:34.664 ERROR 8475 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [PRIMARY]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '3' for key 'PRIMARY'

主键与名称验证有什么关系? 我能做错什么?

谢谢,

Eunito。

0 个答案:

没有答案