Spring Boot应用API请求报错:H2数据库找不到PROFILE_ID列
问题排查:H2数据库Column "USER0_.PROFILE_ID" not found错误
核心原因
User实体类通过@OneToOne关联UserProfile时,用@JoinColumn(name="profile_id")指定了关联字段为profile_id,但data.sql脚本中创建的表字段是USER_PROFILE,导致Hibernate查询时找不到对应列。
分步解决
1. 修正数据库表结构
修改data.sql中的建表语句,将USER_PROFILE字段改为PROFILE_ID,并调整字段类型为与UserProfile主键匹配的整数类型:
DROP TABLE IF EXISTS `TABLE_USER`; CREATE TABLE `TABLE_USER`( `USER_NAME` varchar(250) NOT NULL, `USER_EMAIL` varchar(250) NOT NULL, `PASSWORD` varchar(250) NOT NULL, `USER_ID` int AUTO_INCREMENT PRIMARY KEY, `PROFILE_ID` int -- 关联UserProfile的主键,类型需保持一致 ); -- 先插入UserProfile数据(需确保UserProfile表已创建),再插入User INSERT INTO `TABLE_USER` (`USER_NAME`, `USER_EMAIL`, `PASSWORD`, `PROFILE_ID`) VALUES ('vitória', 'vitoria@gmail.com', 'testing', 1);
2. 确保UserProfile实体与表存在
确认UserProfile实体类定义正确,且对应表已创建(JPA自动建表或通过data.sql初始化):
@Entity @Table(name="user_profile") public class UserProfile implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Integer id; // 其他业务字段 }
3. 清理冗余JPA配置
application.properties中重复配置了DDL自动更新,移除其中一个:
spring.datasource.driverClassName=org.h2.Driver spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.username=sa spring.datasource.password= spring.jpa.database-platform=org.hibernate.dialect.H2Dialect spring.jpa.hibernate.ddl-auto=update spring.jpa.defer-datasource-initialization=true spring.h2.console.enabled=true spring.h2.console.path=/h2-console spring.application.name=currency-exchange-service server.port= 8080 spring.jpa.show-sql=true spring.jpa.properties.hibernate.format_sql=true # 移除重复配置:spring.jpa.properties.hibernate.hbm2ddl.auto=update
4. 修复Controller逻辑错误
- 邮箱校验接口参数错误:路径参数需用
@PathVariable接收,而非直接传入User对象
@GetMapping(value="emails/{email}") public ResponseEntity<Boolean> doesEmailExist(@PathVariable String email){ boolean exists = doesUserExist(email); return ResponseEntity.ok(exists); }
- 新增用户逻辑错误:当用户不存在时,应保存传入的user对象而非null的entity
@PostMapping public ResponseEntity<String> insert(@RequestBody User user){ User entity=repo.findByEmail(user.getEmail()); if(entity==null) { repo.save(user); // 修正为保存传入的user return ResponseEntity.status(HttpStatus.CREATED).body("USER SUCESSFULLY CREATED"); }else { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("AN USER ASSOCIATED TO THIS EMAIL ALREADY EXISTS"); } }
- 更新密码方法错误:误将设置密码写为
setName,需修正为setPassword,且应先查询数据库用户再更新
@PutMapping(value="/updatePassword/{id}") public ResponseEntity<User> updatePassword(@PathVariable Integer id, @RequestBody User user){ User existingUser = repo.findById(id).orElseThrow(() -> new RuntimeException("User not found")); existingUser.setPassword(user.getPassword()); User updatedPassword = repo.save(existingUser); return ResponseEntity.ok().body(updatedPassword); }
- 更新名称方法优化:同样需先查询数据库用户,避免覆盖其他字段
@PutMapping(value="/updateName/{id}") public ResponseEntity<User> updateName(@PathVariable Integer id, @RequestBody User user){ User existingUser = repo.findById(id).orElseThrow(() -> new RuntimeException("User not found")); existingUser.setName(user.getName()); User updatedName = repo.save(existingUser); return ResponseEntity.ok().body(updatedName); }
内容的提问来源于stack exchange,提问作者vitoriac
相关产品推荐
相关产品推荐

