【问题标题】:Java Spring JPA DB. Connections between tablesJava Spring JPA 数据库。表之间的连接
【发布时间】: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


    【解决方案1】:

    我没有包含任何验证注释,但您需要做的是:

    对于AppUser 类:

    @Table(name = "users")
    @Entity
    @Data
    public class AppUser {
    
        @Id
        @SequenceGenerator(name = "user_sequence", sequenceName = "user_sequence", allocationSize = 1)
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_sequence")
        private Long id;
    
        private String firstName;
    
        private String email;
    
        private String password;
    
        @Enumerated(EnumType.STRING)
        private AppUserRole appUserRole;
    
        private Boolean locked;
    
        private Boolean enabled;
    
        @ToString.Exclude
        @EqualsAndHashCode.Exclude
        @OneToOne(mappedBy = "appUser", cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = false)
        private Cart cart;
    
    }
    

    对于Cart 类:

    @Table(name = "carts")
    @Entity
    @Data
    public class Cart {
    
        @Id
        private Long id;
    
        @ToString.Exclude
        @EqualsAndHashCode.Exclude
        @OneToOne(fetch = FetchType.LAZY)
        @MapsId
        private AppUser appUser;
    
        @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
        @JoinColumn(name = "cart_id")
        private Set<Book> books = new HashSet<>();
    
    }
    

    最后是Book 类:

    @Data
    @Entity
    @Table(name = "books")
    public class Book {
    
        @Id
        @GeneratedValue(generator = "book_sequence", strategy = GenerationType.SEQUENCE)
        @SequenceGenerator(name = "book_sequence", sequenceName = "book_sequence", allocationSize = 1)
        private Long id;
    
        private String name;
    
        private String author;
    
        private String publisher;
    
        private String plot;
    
    }
    
    

    您可以看到AppUser 对象与Cart 对象具有one-to-one 关系,而one-to-many 对象又与Book 具有单向one-to-many 关系。

    希望对你有所帮助。

    【讨论】:

      猜你喜欢
      • 2022-01-11
      • 2022-09-28
      • 2022-11-19
      • 2016-12-12
      • 2016-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多