我在hibernate和一对一Map方面遇到了一些问题。
我的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;
}
CustomerController类
@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);
}
}
customerservice类
@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!");
}
}
我尝试使用postman发送这些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
1条答案
按热度按时间tag5nh1u1#
不知道是什么目的
Customer customerNic = customerRepo.findCustomerByNic(dto.getNic());
是的,但既然你没有表现出来,很难说是怎么回事。你试过用它吗customer
在进入循环之前,您保留了哪些内容?