gpt4 book ai didi

java - 在 Hibernate 一对一关系中保存外键的问题

转载 作者:行者123 更新时间:2023-11-30 05:38:18 24 4
gpt4 key购买 nike

我有以下一对一的 Hibernate 关系:

用户

@Entity
@Table(name=User.TABLE_NAME)
public class User {

public static final String TABLE_NAME = "user";

@Id
@GeneratedValue(generator="hibernate-uuid")
@GenericGenerator(name="hibernate-uuid", strategy="uuid2")
@Column(name="id", updatable=false, nullable=false)
private String id;

@OneToOne(mappedBy="user", fetch = FetchType.EAGER, cascade=CascadeType.ALL, orphanRemoval=true)
private Login login;

@Column(name="firstName", nullable=false)
private String firstName;

@Column(name="lastName", nullable=false)
private String lastName;

@Column(name="enabled")
private boolean enabled;

@Column(name="startDate", nullable=false)
@NotNull
private Date startDate;

@Column(name="expirationDate", nullable=false)
@NotNull
private Date expirationDate;

// GETTERS AND SETTERS HERE
}

登录

@Entity
@Table(name=Login.TABLE_NAME)
public class Login {
public static final String TABLE_NAME = "login";

@Id
@GeneratedValue(generator="hibernate-uuid")
@GenericGenerator(name="hibernate-uuid", strategy="uuid2")
@Column(name="id", updatable=false, nullable=false)
private String id;

@OneToOne
@JoinColumn(name="user", referencedColumnName="id", nullable=false)
private User user;

@Column(name="email", nullable=false)
private String email;

@Column(name="password", nullable=false)
private String password;

@Column(name="passwordHash", nullable=false)
private String passwordHash;

@Column(name="token")
private String token;

@Column(name="tokenExpirationDate")
private Date tokenExpirationDate;

@Column(name="lastLogin")
private Date lastLogin;

// GETTERS AND SETTERS HERE
}

用户 DTO

public class UserDTO {
private LoginDTO loginDTO = new LoginDTO();

@Override
public User to(User user) {
user.setFirstName(this.getFirstname());
user.setLastName(this.getLastname());
loginDTO.setEmail(this.getEmail());
Login login = loginDTO.to(new Login());
user.setLogin(login);
user.setStartDate(this.getStartDate());
user.setExpirationDate(this.getExpirationDate());
user.setLogicallyDeleted(this.getLogicallyDeleted());
return company;
}

@Override
public void from(User user) {
this.setFirstname(user.getFirstName());
this.setLastname(user.getLastName());
this.setEmail(user.getLogin().getEmail());
}

public LoginDTO getLoginDTO() {
return loginDTO;
}

public void setLoginDTO(LoginDTO loginDTO) {
this.loginDTO = loginDTO;
}

}

登录DTO

public class LoginDTO {
private String email;

public void from(Login login) {
this.setEmail(login.getEmail());
}

public Login to(Login login) {
login.setEmail(this.getEmail());
return login;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}

}

休息 Controller

我使用 Spring Data,因此 Service 和 Repository 类在本文中被排除,因为它们是不言自明的

@RestController
public class CompanyController extends UserController{

private static final String PATH_CREATE_USER = "/user/save";

@Autowired
private UserService userService;

@PostMapping(PATH_CREATE_USER)
public ResponseEntity<?> save(@RequestBody CompanyDTO dto) {
User user= dto.to(new User());
user.getLogin().setPassword(dto.getPassword());

// Password as text for testing purposes.
user.getLogin().setPasswordHash("haasssshhhhh");
userService.save(company);

return new ResponseEntity<>("", HttpStatus.CREATED);
}
}

我从 Postman 发布以下数据:

{
"firstname": "ben",
"lastname": "moore",
"email": "example@mail.com",
"password": "pass",
"startDate":"2018-06-20T00:00:00",
"expirationDate": "2018-10-20T00:00:00"
}

我得到的结果是 user 表中的所有列都按预期保存。除了留空的 user 列之外,login 表中的列均已保存。我期望 login 表中的 user 列填充 user 表的主键。

最佳答案

我认为您只是没有设置用户和登录名之间的相互引用。

在您的数据传输对象中,我看到您在用户上设置登录名,但我没有看到您在登录名上设置用户的位置。

user.setLogin(login);

我认为你需要

login.setUser(user);

关于java - 在 Hibernate 一对一关系中保存外键的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56200203/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com