映射实体再次保存 - 问题
mapped entity saved again - issue
每当我使用 cartRepository.save(购物车) 用户再次插入用户 table ?(购物车有一个字段用户)。
如何不再保存用户?
用户class:
@Entity
@Table(name="users")
public class Users {
@Id
@GeneratedValue
private int id;
@Column(name="nameofuser")
//@UniqueElements
private String nameofuser;
@Column(name="password")
private String password;
@OneToOne(mappedBy="user",cascade = {CascadeType.ALL})
Cart cart;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNameofuser() {
return nameofuser;
}
public void setNameofuser(String nameofuser) {
this.nameofuser = nameofuser;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
购物车实体:
@Entity
@Table(name="cart")
public class Cart {
@Id
@GeneratedValue
@Column(name="id")
int id;
@OneToOne(cascade = CascadeType.ALL)
Users user;
public Users getUser() {
return user;
}
public void setUser(Users user) {
this.user = user;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
购物车服务class:
package com.praful.lastTry.Services;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.praful.lastTry.MenuRepository.CartRepository;
import com.praful.lastTry.Models.Cart;
import com.praful.lastTry.Models.Users;
@Service
public class CartService {
@Autowired CartRepository cartRepository;
public void bindingWithUser(Users user) {
Cart cart=new Cart();
Users user1=new Users();
cart.setUser(user1);
System.out.println(" from here==================================");
cartRepository.save(cart);
System.out.println("till here ==================================");
}
}
似乎在你的 bindingWithUser
函数中你传递了一个 User
对象 user
但在函数内部,你正在初始化一个新的 User
对象 user1
并在保存之前将其设置为 cart
对象
whenever i am using cartRepository.save(cart) user is again inserted in user table
因为这段代码:
Users user1=new Users();
每次发送一个新用户。
将其更改为:
public void bindingWithUser(Users user) {
Cart cart=new Cart();
Users user1= getUser();
cart.setUser(user1);
System.out.println(" from here==================================");
cartRepository.save(cart);
System.out.println("till here ==================================");
}
private Users getUser(){
//return logged in user or your logic to get user which is already there in Database
}
每当我使用 cartRepository.save(购物车) 用户再次插入用户 table ?(购物车有一个字段用户)。
如何不再保存用户?
用户class:
@Entity
@Table(name="users")
public class Users {
@Id
@GeneratedValue
private int id;
@Column(name="nameofuser")
//@UniqueElements
private String nameofuser;
@Column(name="password")
private String password;
@OneToOne(mappedBy="user",cascade = {CascadeType.ALL})
Cart cart;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNameofuser() {
return nameofuser;
}
public void setNameofuser(String nameofuser) {
this.nameofuser = nameofuser;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
购物车实体:
@Entity
@Table(name="cart")
public class Cart {
@Id
@GeneratedValue
@Column(name="id")
int id;
@OneToOne(cascade = CascadeType.ALL)
Users user;
public Users getUser() {
return user;
}
public void setUser(Users user) {
this.user = user;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
购物车服务class:
package com.praful.lastTry.Services;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.praful.lastTry.MenuRepository.CartRepository;
import com.praful.lastTry.Models.Cart;
import com.praful.lastTry.Models.Users;
@Service
public class CartService {
@Autowired CartRepository cartRepository;
public void bindingWithUser(Users user) {
Cart cart=new Cart();
Users user1=new Users();
cart.setUser(user1);
System.out.println(" from here==================================");
cartRepository.save(cart);
System.out.println("till here ==================================");
}
}
似乎在你的 bindingWithUser
函数中你传递了一个 User
对象 user
但在函数内部,你正在初始化一个新的 User
对象 user1
并在保存之前将其设置为 cart
对象
whenever i am using cartRepository.save(cart) user is again inserted in user table
因为这段代码:
Users user1=new Users();
每次发送一个新用户。
将其更改为:
public void bindingWithUser(Users user) {
Cart cart=new Cart();
Users user1= getUser();
cart.setUser(user1);
System.out.println(" from here==================================");
cartRepository.save(cart);
System.out.println("till here ==================================");
}
private Users getUser(){
//return logged in user or your logic to get user which is already there in Database
}