我现在已经花了最后两天的时间来确定为什么删除单个行存在问题。这个问题让我感到困惑,因为没有任何错误!我可以肯定的事情是:1)Postgres DB中只有一个数据行。我已经硬编码了需要删除的主键。
用户注册控制器
@RestController
@RequestMapping(value = "registration")
public class RegistrationController {
@Autowired
UserRegistrationService registrationService;
@PostMapping(value = "/register")
public void registerUser(@RequestBody UserInformation accountInfo) {
// Perform some back validation.
registrationService.createUserInformation(accountInfo);
}
@DeleteMapping(value = "/register/{id}")
public void deleteUser(@PathVariable("id") Integer userId) {
// Delete the registration account
registrationService.deleteUserInformation(userId);
}
}
用户注册服务
@Service
public class UserRegistrationService {
@Autowired
UserInformationRepository userInformationRepository;
/**
* Accepts a user's registration and attempts to save it to the database.
* @param info
*/
public void createUserInformation(UserInformation info) {
userInformationRepository.save(info);
}
public void deleteUserInformation(Integer userId) {
UserInformation user = userInformationRepository.findById(userId).get();
userInformationRepository.delete(user);
// userInformationRepository.deleteById(userId);
}
}
用户信息存储库
没有自定义方法签名。
用户信息模型
@Entity
@Table(name = "user_info")
public class UserInformation {
@Id
@Generated(value = "GenerationType.AUTO")
private int id;
@OneToOne(targetEntity = PersonalInfo.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
@JoinColumn(name = "personal_info")
private PersonalInfo personalInfo;
@Column(name = "username")
private String username;
@Column(name = "password")
private String password;
@Column(name = "email")
private String emailAddress;
+ Setters and Getters
}
个人信息模型
@Entity
@Table(name = "personal_info")
public class PersonalInfo {
@Id
@Generated(value="GenerationType.AUTO")
@Column(name = "info_id")
private int infoId;
@Column(name = "firstname")
private String firstName;
@Column(name = "lastname")
private String lastName;
@Column(name = "street")
private String street;
@Column(name = "city")
private String city;
@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "state_id")
private States state;
@Column(name = "zipcode")
private String zipCode;
@Column(name = "mobile_number")
private String mobileNumber;
@Column(name = "home_number")
private String homeNumber;
@Column(name = "work_number")
private String workNumber;
@OneToOne(mappedBy = "personalInfo", orphanRemoval = true)
private UserInformation user;
+ Setters and Getters
}
状态模型
@Entity
@Table(name="states")
public class States {
@Id
//@Generated(value="GenerationType.AUTO")
@Column(name = "id")
private Long id;
@Column(name="abbrev")
private String abbreviation;
@Column(name="state_name")
private String stateName;
@OneToMany(mappedBy = "state")
private List<PersonalInfo> personalInfos;
+ Setters and Getters
}
我最初尝试使用deleteById,但似乎没有任何作用。我觉得我很想念我。
更新: 我在UserInformation模型上将“ id”值上的数据类型从int更改为Integer。当我使用此更改运行删除测试时,记录将被删除。但是,然后我无法插入记录。似乎与数据类型以及Postgres数据库如何处理它有关。有人遇到过这种问题吗?
使用Integer数据类型插入时的堆栈跟踪
at com.deck.grocerymanager.Integration.RegistrationTest.userRegTest(RegistrationTest.java:82)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)