如何在SpringMVC的@joincolumn中插入数据
我有两个表customerdetails
和product
,我想从cocers
表中添加它> @joincolumn(order_id) customerdetails
表中的列。
customerDetails.java
@Entity
@Table(name = "CustomerDetails")
public class CustomerDetails {
@Id
@GeneratedValue
@Column(name="CUSTOMER_ID")
private Long custid;
@Column(name="CUSTOMER_NAME")
private String customerName;
@Column(name="EMAIL")
private String email;
@Column(name="ADDRESS")
private String address;
@Column(name="PHONENO")
private String phoneno;
public CustomerDetails() {
}
@Override
public String toString() {
return "CustomerDetails [custid=" + custid + ", customername=" + customerName + ", email=" + email
+ ", address=" + address + ", phoneno=" + phoneno + "]";
}
public CustomerDetails(String customername, String email, String address, String phoneno) {
super();
this.customerName = customername;
this.email = email;
this.address = address;
this.phoneno = phoneno;
}
public Long getCustid() {
return custid;
}
public void setCustid(Long custid) {
this.custid = custid;
}
public String getName() {
return customerName;
}
public void setName(String name) {
this.customerName = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return address;
}
public void setPassword(String password) {
this.address = password;
}
public String getPhoneno() {
return phoneno;
}
public void setPhoneno(String phoneno) {
this.phoneno = phoneno;
}
}
product.java
@Entity
@Table(name="Product")
public class Product {
@Id
@GeneratedValue
@Column(name="PRODUCT_ID")
private Long productId;
@Column(name="PRODUCT_NAME")
private String productName;
@Column(name="PRODUCT_BRAND")
private String productBrand;
@Column(name="PRODUCT_PRICE")
private double productPrice;
@OneToOne
private CustomerDetails cd;
public Product(Long productId, String productName, String productBrand, double productPrice, CustomerDetails cd) {
super();
this.productId = productId;
this.productName = productName;
this.productBrand = productBrand;
this.productPrice = productPrice;
this.cd = cd;
}
public Product(String productName, String productType, double productPrice) {
super();
this.productName = productName;
this.productBrand = productType;
this.productPrice = productPrice;
}
public Long getProductId() {
return productId;
}
public void setProductId(Long productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductBrand() {
return productBrand;
}
public void setProductBrand(String productType) {
this.productBrand = productType;
}
public double getProductPrice() {
return productPrice;
}
public void setProductPrice(double productPrice) {
this.productPrice = productPrice;
}
public CustomerDetails getCd() {
return cd;
}
public void setCd(CustomerDetails cd) {
this.cd = cd;
}
public Product() {
//super();
}
@Override
public String toString() {
return "Product [productId=" + productId + ", productName=" + productName + ", productType=" + productBrand
+ ", productPrice=" + productPrice + "]";
}
}
customerDetails
@Repository
public interface CdRepo extends JpaRepository<CustomerDetails, Long>
{
}
使用getCustid()
我现在获得了
@Repository
public interface ProductRepo extends JpaRepository<Product, Long>
{
}
客户的ID ,
@Service
@Transactional
public class CustomerService {
private final CdRepo cdRepo;
@Autowired
public CustomerService(CdRepo cdRepo) {
this.cdRepo = cdRepo;
}
public void saveCustomer(CustomerDetails cd)
{
cdRepo.save(cd);
}
}
当前
@RequestMapping(value = {"/addCustomerDetails"}, method = RequestMethod.POST)
public ModelAndView addCustomerDetails(CustomerDetails cd)
{
customerService.saveCustomer(cd);
System.out.println(cd.getCustid());
ModelAndView model = new ModelAndView();
model.setViewName("homepage");
return model;
}
我想将该ID插入@joincolumn(order_id)
I have two tables CustomerDetails
and Product
, I want to fetch customerid
from customer
table and add it to @joincolumn(order_id)
column in same CustomerDetails
table.
CustomerDetails.java
@Entity
@Table(name = "CustomerDetails")
public class CustomerDetails {
@Id
@GeneratedValue
@Column(name="CUSTOMER_ID")
private Long custid;
@Column(name="CUSTOMER_NAME")
private String customerName;
@Column(name="EMAIL")
private String email;
@Column(name="ADDRESS")
private String address;
@Column(name="PHONENO")
private String phoneno;
public CustomerDetails() {
}
@Override
public String toString() {
return "CustomerDetails [custid=" + custid + ", customername=" + customerName + ", email=" + email
+ ", address=" + address + ", phoneno=" + phoneno + "]";
}
public CustomerDetails(String customername, String email, String address, String phoneno) {
super();
this.customerName = customername;
this.email = email;
this.address = address;
this.phoneno = phoneno;
}
public Long getCustid() {
return custid;
}
public void setCustid(Long custid) {
this.custid = custid;
}
public String getName() {
return customerName;
}
public void setName(String name) {
this.customerName = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return address;
}
public void setPassword(String password) {
this.address = password;
}
public String getPhoneno() {
return phoneno;
}
public void setPhoneno(String phoneno) {
this.phoneno = phoneno;
}
}
Product.java
@Entity
@Table(name="Product")
public class Product {
@Id
@GeneratedValue
@Column(name="PRODUCT_ID")
private Long productId;
@Column(name="PRODUCT_NAME")
private String productName;
@Column(name="PRODUCT_BRAND")
private String productBrand;
@Column(name="PRODUCT_PRICE")
private double productPrice;
@OneToOne
private CustomerDetails cd;
public Product(Long productId, String productName, String productBrand, double productPrice, CustomerDetails cd) {
super();
this.productId = productId;
this.productName = productName;
this.productBrand = productBrand;
this.productPrice = productPrice;
this.cd = cd;
}
public Product(String productName, String productType, double productPrice) {
super();
this.productName = productName;
this.productBrand = productType;
this.productPrice = productPrice;
}
public Long getProductId() {
return productId;
}
public void setProductId(Long productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductBrand() {
return productBrand;
}
public void setProductBrand(String productType) {
this.productBrand = productType;
}
public double getProductPrice() {
return productPrice;
}
public void setProductPrice(double productPrice) {
this.productPrice = productPrice;
}
public CustomerDetails getCd() {
return cd;
}
public void setCd(CustomerDetails cd) {
this.cd = cd;
}
public Product() {
//super();
}
@Override
public String toString() {
return "Product [productId=" + productId + ", productName=" + productName + ", productType=" + productBrand
+ ", productPrice=" + productPrice + "]";
}
}
CustomerDetails repository
@Repository
public interface CdRepo extends JpaRepository<CustomerDetails, Long>
{
}
Product repository
@Repository
public interface ProductRepo extends JpaRepository<Product, Long>
{
}
CustomerService
@Service
@Transactional
public class CustomerService {
private final CdRepo cdRepo;
@Autowired
public CustomerService(CdRepo cdRepo) {
this.cdRepo = cdRepo;
}
public void saveCustomer(CustomerDetails cd)
{
cdRepo.save(cd);
}
}
controller
@RequestMapping(value = {"/addCustomerDetails"}, method = RequestMethod.POST)
public ModelAndView addCustomerDetails(CustomerDetails cd)
{
customerService.saveCustomer(cd);
System.out.println(cd.getCustid());
ModelAndView model = new ModelAndView();
model.setViewName("homepage");
return model;
}
In controller using getCustid()
I'm getting current customer's id now I want to insert that id into @joinColumn(order_id)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解,则要将产品分配给用户(例如客户)。
对于
@OnetoOne
关系,您不需要private customerdetails cd;
在产品类中。尽管我不知道为什么您会以这种方式实施这种事情!通常,如果您想将两件事分配在一起,假设您想将产品分配给用户,以便该产品适合该用户,则应从存储库或任何地方找到
product
obj产品和用户都必须具有DB生成的ID),然后将其分配给user.product
。产品服务
客户服务
控制器
在每个实体中编写Getters和Setter,或使用Lombok注释
@Data
。通常,当我想用用户和产品部署电子商务时。我使用
用户
,购物车
,产品
模型。上面代码的问题是,如果将该产品分配给用户,则仅适用于该用户。如果其他用户想要相同的产品,则必须为其创建所有这些产品。解决方案将使用产品作为系列或模型。
想象一下您想创建一个网站以出售咖啡包的情况。您只有两种类型的包装可供出售。您可以为这些软件包创建一个类似的产品,并为每个软件包创建
数量
。然后在购物车模型中创建@onetomany
与产品中的关系。它将创建一个JOIN表,以将任何可能的产品ID存储在此处使用CART ID。之后,在您的购物车实体中创建@manytoone
关系,@onetomany
在您的用户实体中。现在,每个购物车仅适用于特定用户,而不适合产品。一些友好的建议:
dbcustomer
用于执行操作。If I've understood correctly, you want to assign a product to a user (e.g Customer).
For a
@OneToOne
relation you don't needprivate CustomerDetails cd;
in product class. Although I don't know why are you implementing such thing in that way at all!Generally If you want to assign two things together, let's say you want to assign a product to a user so that the product would be for that user, you should find the
product
obj from repository or any where (both product and user must have an id generated by db) and then assign it touser.product
.product service
customer service
controller
Write getters and setters in each entity or use Lombok annotation
@Data
.Usually when I want to deploy an ecommerce with user and product. I use
user
,cart
,product
model.The problem with the code above is that if you assign that product to a user, it's ONLY for that user. if other users want the same product you have to create all of those products for them. Solution to that would be using product as a series or a model.
Imagine a situation that you want to create a website to sell coffee packages. you only have two type of package to sell. you can create an entity like product for those packages with a
quantity
for each. Then create a@OneToMany
relationship in your cart model to products. It will create a join table to store any possible product id there with cart id. After that, create a@ManyToOne
relationship in your cart entity and@OneToMany
in your user entity. Now each cart is only for a specific user and not the products.Some Friendly Advice:
dbCustomer
for doing operations.