得到一些问题与桌子布线。我需要为每个用户uniq购物车,在那里我将存储书籍。
当我在DB中打开AppUser表时,“cart_id”列始终为NULL。
购物车表只有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;
}
}
我不包括任何验证注释,但您需要做的是:
对于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;
}
对于购物车
类:
@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<>();
}
@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
对象具有一对一
关系,而cart
对象又与book
具有单向一对多
关系。
希望这能帮到你。
主键:Rev/Exp ID 这是我的模型的一个示例: 这是DBContext:公共类AppDbContext:标识符DbContext{公共AppDbContext(DbContextOptions选项): base(选项){} 以下是控制器中的创建操作: 最后,创建视图: 我的问题是如何关联用户和他们输入的数据?因此,Rev或Exp中的记录必须有一列,其中包含输入该数据的用户的ID,以便捕获这些
问题内容: 在MySQL中,我有两个不同的数据库-我们姑且称之为 一个 和 乙 。 是否可以在数据库 A 中的表与数据库 B中 的表之间执行 联接 ? 问题答案: 是的,假设该帐户具有适当的权限,则可以使用: 您只需要在表引用前面加上它所驻留的数据库的名称即可。
是否可以使用databricks连接来切换工作区? 我目前正在尝试切换: 但这会返回以下错误:< code>AnalysisException:无法修改Spark配置的值:spark.driver.host
本文向大家介绍数据仓库和运营数据库之间的区别,包括了数据仓库和运营数据库之间的区别的使用技巧和注意事项,需要的朋友参考一下 数据仓库是用于结构化,经过过滤的数据的存储库,该数据已针对特定目的进行了处理。数据软件从多个来源收集数据,并使用ETL流程转换数据,然后将其加载到数据仓库中以用于业务目的。 运营数据库是那些数据经常更改的数据库。它们主要设计用于大量数据交易。它们是数据仓库的源数据库,用于维护
本文向大家介绍MySql学习day03:数据表之间的连接、查询详解,包括了MySql学习day03:数据表之间的连接、查询详解的使用技巧和注意事项,需要的朋友参考一下 主键: 关键字:primary key 特点:不能为null,并且唯一。 主键分类: 逻辑主键:例如ID,不代表实际的业务意义,只是用来唯一标识一条记录(推荐) 业务主键:例如username,参与实际的业务逻辑。 主键使用方式:
问题内容: 我们正在多家商店中运行带有MySql后端的Java PoS(销售点)应用程序。我想保持商店中的数据库与主机服务器上的数据库同步。 商店中发生某些更改时,应在主机服务器上对其进行更新。我该如何实现? 问题答案: 复制不是很难创建。 这里有一些很好的教程: http://aciddrop.com/2008/01/10/step-by-step-how-to-setup-mysql-data