You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring MVC含特殊字符姓名入库报错:主键重复与姓名验证关联疑问

Primary Key Duplicate Error When Inserting Names with Special Characters (Regex Validation Passed)

I'm trying to add user records with names containing special characters like Patrícia or José. For validation, I'm using the regex pattern ^[a-zA-ZÀ-ÿ]+, which I've verified works correctly in a regex testing tool.

Here's how the primary key and name field are defined in my User model:

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

// Other fields omitted...

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

// Getters, setters, and constructors are implemented

When inserting names without special characters (like Patricia or Jose), the validation passes and the user is saved to the database successfully. However, when I try to insert names with special characters, I get a primary key duplicate error:

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'

I'm confused about how a primary key duplicate error relates to the name validation. Where could I have gone wrong?

Thanks,
Eunito


内容的提问来源于stack exchange,提问作者Eunito

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:31:40