java - 嵌套异常是 org.hibernate.id.IdentifierGenerationException:必须在调用 save() 之前手动分配此类的 id:
问题描述
我在使用 Hibernate 和一对一映射时遇到了一些问题。
我的 DTO 课程是这样的:
客户DTO
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class CustomerDTO {
private String nic;
private String name;
private String address;
private String contact;
private ArrayList<UserDTO> user = new ArrayList<>();
用户DTO
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class UserDTO {
private String email;
private String password;
private String role;
private String lastLogged;
}
我的实体类是这样的
顾客
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class Customer {
@Id
private String nic;
private String name;
private String address;
private String contact;
@OneToOne(mappedBy = "customer", cascade = CascadeType.ALL)
private User user;
用户
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class User {
@Id
private String email;
private String password;
private String role;
private String lastLogged;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "cusNIC", referencedColumnName = "nic", nullable = false)
private Customer customer;
}
客户控制器类
@RestController
@RequestMapping("/api/v1/customer")
@CrossOrigin
public class CustomerController {
@Autowired
CustomerService customerService;
@PostMapping(consumes = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity saveCustomer(@RequestBody CustomerDTO dto){
customerService.saveCustomer(dto);
StandradResponse success = new StandradResponse(200, "success", null);
return new ResponseEntity(success, HttpStatus.OK);
}
}
客户服务类
@Transactional
@Service
public class CustomerServiceImpl implements CustomerService {
@Autowired
CustomerRepo customerRepo;
@Autowired
UserRepo userRepo;
@Autowired
ModelMapper mapper;
@Override
public void saveCustomer(CustomerDTO dto) {
if(!customerRepo.existsById(dto.getNic())){
Customer customer = mapper.map(dto, Customer.class);
customerRepo.save(customer);
for (UserDTO ud : dto.getUser()){
if(!userRepo.existsById(ud.getEmail())){
UserDTO userDTO = new UserDTO(ud.getEmail(),ud.getPassword(),ud.getRole(),ud.getLastLogged());
User user = new User(userDTO.getEmail(), userDTO.getPassword(), userDTO.getRole(), userDTO.getLastLogged(), customer);
//User user = mapper.map(userDTO, User.class);
userRepo.save(user);
}else {
throw new RuntimeException("Email is already exist!");
}
}
}else{
throw new RuntimeException("Customer is already exist!");
}
}
我尝试使用邮递员发送这些 Json 值
{
"nic" : "55665v",
"name" : "anyname",
"address" : "no 20,56 text",
"contact" : "54673453",
"user": [{
"email":"text@gmail.com",
"password":"1234",
"role":"driver",
"lastLogged":"sunday"
}]
}
每次我调用我的函数时,我都会得到
ids for this class must be manually assigned before calling save(): lk.EasyCarRental.backend.entity.Customer; nested exception is org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): lk.EasyCarRental.backend.entity.Customer
我不想自动生成 ID。我不会手动输入id
解决方案
不知道目的Customer customerNic = customerRepo.findCustomerByNic(dto.getNic());
是什么,但由于你没有表现出来,所以很难说发生了什么。您是否尝试过使用customer
您在进入循环之前坚持的内容?
推荐阅读
- angular - 在 BehaviourSubject 中传递多个值
- c# - 有没有办法在 C# 中获取击键的时间戳?
- r - 返回数据框中所有考试都超过某个值的学生姓名
- mysql - 如果使用内部连接 MySql?
- r2dbc - 批量插入支持
- javascript - 在 TypeScript 中使用泛型
- c# - Graph API - 使用 .Net Core 3.1 添加模式扩展
- java - Pluggable Type Checkers 和 Checkers 框架,真的需要所有这些设置吗?
- redux - Redux:未捕获的错误:操作可能没有未定义的“类型”属性。你有没有拼错一个常数?
- swift - 使用 webview 后如何使用 segue?