我正在使用Spring Boot + hibernate制作书店休息api,我不知道该怎么办。首先创建用户(使用DTO未在此处发布):
@Entity
@Table(name = "app_user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id", nullable = false, updatable = false, unique = true)
private Long id;
@Column(name = "email", nullable = false, unique = true)
private String email;
@Column(name = "password_hash", nullable = false)
private String passwordHash;
@OneToOne
@JoinColumn(name = "customer_id")
private Customer customer;
@Column(name = "role", nullable = false)
@Enumerated(EnumType.STRING)
private Role role;
// jpa requirement
public User() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPasswordHash() {
return passwordHash;
}
public void setPasswordHash(String passwordHash) {
this.passwordHash = passwordHash;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
}
然后他可以登录并搜索他感兴趣的书籍。当他想要订购它们时(使用包含他的书籍的JSON的POST),他应该在我的客户实体中输入他的详细信息(使用DTO): / p>
@Entity
@Table(name = "customer")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "customer_id", nullable = false, updatable = false, unique = true)
private Long id;
@Column(name = "first_name", nullable = false)
private String firstName;
@Column(name = "last_name", nullable = false)
private String lastName;
@Column(name = "phone_number", nullable = false)
private String phoneNumber;
@ManyToOne
@JoinColumn(name = "address_id")
private Address address;
@OneToOne
@JoinColumn(name = "user_id")
private User user;
// jpa requirement
public Customer() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
这是一个好方法吗?我不知道如何将用户与客户联系起来 - >客户有完成订单的所有细节,而用户是jsut注册的人,它的工作就像在网上商店,但我;我不确定我是否正确链接他们,我的思维过程是好的。有人详细说明了一点吗?
编辑: 澄清:
用户注册并且只有电子邮件和密码 - POST @ / users
用户添加他的客户详细信息(无地址)POST @ / users / 1 / customer:找到当前登录的用户 - >添加客户在用户表中设置customer_id(OneToOne连接)
用户添加了他的地址详细信息POST @ users / 1 / customer / address:找到当前登录的用户 - >通过customer_id找到客户,然后通过address_id
所有3个使用不同的DTO - 这是一个好方法吗?
答案 0 :(得分:0)
我认为你可以重新塑造你的实体:
客户是用户
客户购买书籍
使用默认属性创建User类。 扩展User类以创建Customer类并在其中添加额外字段。 创建一个Book类。 将Book类的ArrayList作为书籍添加到Customer类。
用户登录后,您已拥有用户详细信息。为用户创建客户对象,并在用户作为客户填写时添加额外的详细信息,将所选书籍添加到客户对象并将客户对象保存到数据库。