我有两个表Users
CREATE TABLE `users` (
`id` BIGINT NOT NULL,
PRIMARY KEY (`id`))
和购物车
CREATE TABLE `carts` (
`id` BIGINT NOT NULL,
`user_id` BIGINT,
PRIMARY KEY (`id`))
ALTER TABLE `carts` ADD CONSTRAINT Cart_User_FK
FOREIGN KEY (`user_id`) REFERENCES `users` (`id`);
我为他们创建实体
@Entity
@Table(name = "users")
public class User{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "user")
private Cart cart;
}
和
@Entity
@Table(name = "carts")
public class Cart{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "user_id")
private User user;
}
我尝试将购物车实例设置为用户并保存用户
Cart cart = new Cart();
cartRepository.save(cart);
user.setCart(cart);
userRepository.save(user);
但休眠时不更新购物车表
从SimpleJpaRepository扩展的存储库
有人可以解释一下如何更新相关对象吗?