【发布时间】:2021-05-28 08:47:56
【问题描述】:
遇到一些表接线问题。我需要为每个用户提供 uniq 购物车,我将在其中存放书籍。
我不明白如何使用 OneToMany/ManyToOne 以及整个画面应该是什么样子。
当我在 DB 中打开 AppUser 表时,“cart_id”列始终为空。
购物车表只有 id 列 - 不确定是否应该这样
谢谢!
@Component
@Getter
@Setter
@EqualsAndHashCode
@NoArgsConstructor
@Entity
public class AppUser implements UserDetails {
@Id
@SequenceGenerator(
name = "user_sequence",
sequenceName = "user_sequence",
allocationSize = 1
)
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "user_sequence"
)
private Long id;
@NotEmpty(message = "Name cannot be empty")
private String firstName;
@Email
private String email;
@NotEmpty
private String password;
@Enumerated(EnumType.STRING)
private AppUserRole appUserRole;
private Boolean locked = false;
private Boolean enabled = false;
@OneToOne
@Autowired
private Cart cart;
public AppUser(String firstName, String email, String password, AppUserRole appUserRole) {
this.firstName = firstName;
this.email = email;
this.password = password;
this.appUserRole = appUserRole;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
SimpleGrantedAuthority authority = new SimpleGrantedAuthority(appUserRole.name());
return Collections.singleton(authority);
}
@Override
public String getPassword(){
return password;
}
@Override
public String getUsername() {
return email;
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return !locked;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return enabled;
}
}
购物车
@Entity
@Component
public class Cart {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToMany
@JoinColumn( name = "books_in_cart")
private final List<Book> books;
public Cart(){
books = new ArrayList<Book>();
}
public List<Book> getBooks() {
return books;
}
public void setBook(Book book){
books.add(book);
}
}
书
@Setter
@Getter
@NoArgsConstructor
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotEmpty(message = "Book's title cannot be empty")
@Size(min = 3, max = 100, message = "Book's title's length should be from 3 to 100")
private String name;
@NotEmpty(message = "Book's author name cannot be empty")
@Size(min = 3, max = 100, message = "Book's author's name should be from 3 to 100")
private String author;
@NotEmpty(message = "Book's publisher cannot be empty")
@Size(min = 3, max = 100, message = "Book's publisher's name should be from 3 to 100")
private String publisher;
@Min(value = 1000, message = "Book publishing year cannot be less than 1000")
@Max(value = 2021, message = "Book publishing year cannot be more than 2021")
private int year;
@NotEmpty(message = "Book's description cannot be empty")
@Size(min = 1, max = 100, message = "Book's description's length cannot be more than 1000")
private String plot;
public Book(String name, String author, String publisher, int year, String plot) {
this.name = name;
this.author = author;
this.publisher = publisher;
this.year = year;
this.plot = plot;
}
}
【问题讨论】:
标签: java spring-boot jpa