【问题标题】:Not able to map fields using Hibernate Mapping无法使用 Hibernate Mapping 映射字段
【发布时间】:2015-09-30 10:41:33
【问题描述】:

我想在表格供应商详细信息和产品详细信息之间建立一对多的关系。就像一个供应商可以拥有多种产品一样。但是当我将数据插入表中时,它会插入所有四个字段,但不会将 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;
    }

}

【问题讨论】:

  • @OneToMany(targetEntity = ProductDetails.class) 可能有帮助吗?你也可以尝试添加@JoinColumn

标签: java mysql hibernate


【解决方案1】:

VendorDetails 有很多 ProductDetails,所以你需要像这样进行一对多的注释:-

@OneToMany(mappedBy="vendordetails") //mappedBy value will be what you    declared //in  ProductDetails class.
private Collection<ProductDetails> productdetail=new ArrayList<ProductDetails>;

并创建 this 的 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。

【讨论】:

  • 因为在你的代码中你想要一对多但是你没有使用任何 Collection 和它的子类。另一件事是您需要提供 @onetomany 的 mappedBy 属性,否则它将创建一个临时表并插入两个表的主键。
【解决方案2】:

我认为你应该替换代码
@OneToMany 私有 ProductDetails productdetail;

@OneToMany 私有集productdetailSet;

并为此创建 setter 和 getter。

您可以访问博客http://gaurav1216.blogspot.in/2014/01/hibernate-tutorial-day-5.html 进行一对多使用注释。

【讨论】:

  • @ManyToOne(cascade = CascadeType.ALL) @JoinColumn(name = "VendorId") private VendorDetails vendordetails;如果我编写此代码,它将生成一个新表,其中包含两个字段 productdetail_productid 和 vendordetail_vendorid
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-05
  • 1970-01-01
  • 2017-10-12
相关资源
最近更新 更多