我想在表供应商详细信息和产品详细信息之间建立一对多的关系。像一个供应商可以有多个产品。但是当我将数据插入表时,它插入了所有四个字段,但没有将vendorid映射到ProductDetail表中 和生成的查询就是这个。
Hibernate: insert into ProductInfo (productCategory, productDetails, productPrice, VendorId) values (?, ?, ?, ?) It shuld map vendor ID also but in table its empty.
VendorDetail.java
package com.cts.entity;
import javax.persistence.*;
@Entity
@Table(name = "VendorInfo")
public class VendorDetails {
@Id
@Column
private Long VendorId;
@OneToMany
private ProductDetails productdetail;
@Column
private String VendorName;
@Column
private String Password;
public String getVendorName() {
return VendorName;
}
public void setVendorName(String vendorName) {
VendorName = vendorName;
}
public Long getVendorId() {
return VendorId;
}
public void setVendorId(Long vendorId) {
VendorId = vendorId;
}
public String getPassword() {
return Password;
}
public void setPassword(String password) {
Password = password;
}
}
ProductDetails.java
package com.cts.entity;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity@Table(name = "ProductInfo")
public class ProductDetails {
@ManyToOne(cascade = CascadeType.ALL)@JoinColumn(name = "VendorId")
private VendorDetails vendordetails;
public ProductDetails() {
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private int productId;
@Column
private String productCategory;
@Column
private String productDetails;
@Column
private String productPrice;
public VendorDetails getVendordetails() {
return vendordetails;
}
public void setVendordetails(VendorDetails vendordetails) {
this.vendordetails = vendordetails;
}
public int getProductId() {
return productId;
}
public void setProductId(int productId) {
this.productId = productId;
}
public String getProductCategory() {
return productCategory;
}
public void setProductCategory(String productCategory) {
this.productCategory = productCategory;
}
public String getProductDetails() {
return productDetails;
}
public void setProductDetails(String productDetails) {
this.productDetails = productDetails;
}
public String getProductPrice() {
return productPrice;
}
public void setProductPrice(String productPrice) {
this.productPrice = productPrice;
}
}
DAO类ProductDetailDaoImpl.java
package com.cts.Dao;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.cts.entity.ProductDetails;
import com.cts.entity.to.ProductDetailsTo;
@Repository
public class ProductDetailDaoImpl implements ProductDetailDao {
@Autowired
SessionFactory sessionFactory;
@Transactional
public boolean saveProductInfo(ProductDetailsTo productTo) {
System.out.println("M in Registration DAO");
System.out.println(productTo.getProductCategory());
System.out.println(productTo.getProductDetails());
System.out.println(productTo.getProductId());
System.out.println(productTo.getProductPrice());
//getting productTo data to entity class
ProductDetails prodet = productTo.getEntity();
System.out.println("Value of product details is:" + prodet.getProductDetails());
sessionFactory.getCurrentSession().save(prodet);
return false;
}
}
答案 0 :(得分:0)
我认为你应该更换代码
@OneToMany
private ProductDetails productdetail;
到
@OneToMany private Set productdetailSet;
为此创建setter和getter。
您可以使用注释访问博客http://gaurav1216.blogspot.in/2014/01/hibernate-tutorial-day-5.html一对多。
答案 1 :(得分:0)
VendorDetails有很多ProductDetails,因此您需要制作一对多的注释: -
@OneToMany(mappedBy="vendordetails") //mappedBy value will be what you declared //in ProductDetails class.
private Collection<ProductDetails> productdetail=new ArrayList<ProductDetails>;
并创建此的setter和getter。
Now in ProductDetails class you need to annotate many to one like this:-
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "VendorId")
private VendorDetails vendordetails;
然后将在表'ProductInfo'中创建一个名为'VendorId'的新列,并声明mappedBy value =“vendordetails”,因此每个供应商ID都将被插入。