0

我正在尝试使用休眠将文本字段中的值插入数据库中,但它不起作用。例如,我想插入到 Product 表中,其中包含以下列:idproduct、idprovider、idcategory、name、quantity。

    Session session = HibernateUtil.getSessionFactory().openSession();
    org.hibernate.Transaction tx = session.beginTransaction();
    Product product = new Product();
    int idProduct = Integer.parseInt(IdProductTextField.getText());
    product.setIdproduct(idProduct);
    session.save(product);
    tx.commit();

这是唯一插入的列,因为在 set 方法中我有一个 int 参数。

但是所有其他列都不起作用。这就是问题所在:

我要在 Product 表中插入的其他列的所有其他设置方法都具有 Class 变量作为参数。例如:

如果我想插入 idprovider,设置方法是

public void setProduct(Product product) {
    this.product = product;
}

而且我真的不知道如何使用这种方法插入idprovider。

我没有多少时间,如果有人能尽快帮助我,我将不胜感激。

我在这里附上 Product.java 和 Provider.java

package sakila.entity;

import java.util.HashSet;
import java.util.Set;

public class Product  implements java.io.Serializable {

 private int idproduct;
 private Istoricprod istoricprod;
 private Provider provider;
 private Category category;
 private String name;
 private Integer quantity;
 private String unitmas;
 private Set comdetals = new HashSet(0);
 private Set stocs = new HashSet(0);
 private Set bondetals = new HashSet(0);

public Product() {
}


public Product(int idproduct) {
    this.idproduct = idproduct;
}
public Product(int idproduct, Istoricprod istoricprod, Provider provider, Category category, String name, Integer quantity, String unitmas, Set comdetals, Set stocs, Set bondetals) {
   this.idproduct = idproduct;
   this.istoricprod = istoricprod;
   this.provider = provider;
   this.category = category;
   this.name = name;
   this.quantity = quantity;
   this.unitmas = unitmas;
   this.comdetals = comdetals;
   this.stocs = stocs;
   this.bondetals = bondetals;
}

public int getIdproduct() {
    return this.idproduct;
}

public void setIdproduct(int idproduct) {
    this.idproduct = idproduct;
}
public Istoricprod getIstoricprod() {
    return this.istoricprod;
}

public void setIstoricprod(Istoricprod istoricprod) {
    this.istoricprod = istoricprod;
}
public Provider getProvider() {
    return this.furnizor;
}

public void setProvider(Provider provider) {
    this.provider = provider;
}
public Category getCategory() {
    return this.category;
}

public void setCategory(Category category) {
    this.category = category;
}

public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}
public Integer getQuantity() {
    return this.quantity;
}

public void setQuantitaty(Integer quantity) {
    this.quantity = quantity;
}
public String getUnitmas() {
    return this.unitmas;
}

public void setUnitmas(String unitmas) {
    this.unitmas = unitmas;
}
public Set getComdetals() {
    return this.comdetals;
}

public void setComdetals(Set comdetals) {
    this.comdetals = comdetals;
}
public Set getStocs() {
    return this.stocs;
}

public void setStocs(Set stocs) {
    this.stocs = stocs;
}
public Set getBondetals() {
    return this.bondetals;
}

public void setBondetals(Set bondetals) {
    this.bondetals = bondetals;
}

}

Provider.java 是

package sakila.entity;

// 由 Hibernate Tools 3.2.1.GA 于 2012 年 4 月 9 日晚上 10:19:40 生成

import java.util.HashSet;
import java.util.Set;


public class Provider  implements java.io.Serializable {


 private int idprovider;
 private String denumire;
 private Integer codfiscal;
 private Integer nrfirma;
 private String stradadom;
 private Integer numardom;
 private String orasdom;
 private String judetdom;
 private Integer telefon;
 private Integer codiban;
 private String banca;
 private Set produses = new HashSet(0);
 private Set chitantas = new HashSet(0);
 private Set comandas = new HashSet(0);
 private Set facturas = new HashSet(0);

public Provider() {
}


public Provider(int idprovider) {
    this.idprovider = idprovider;
}
public Provider(int idprovider, String denumire, Integer codfiscal, Integer nrfirma, String stradadom, Integer numardom, String orasdom, String judetdom, Integer telefon, Integer codiban, String banca, Set produses, Set chitantas, Set comandas, Set facturas) {
   this.idprovider = idprovider;
   this.denumire = denumire;
   this.codfiscal = codfiscal;
   this.nrfirma = nrfirma;
   this.stradadom = stradadom;
   this.numardom = numardom;
   this.orasdom = orasdom;
   this.judetdom = judetdom;
   this.telefon = telefon;
   this.codiban = codiban;
   this.banca = banca;
   this.produses = produses;
   this.chitantas = chitantas;
   this.comandas = comandas;
   this.facturas = facturas;
}

public int getIdprovider() {
    return this.idprovider;
}

public void setIdprovider(int idprovider) {
    this.idprovider = idprovider;
}
public String getDenumire() {
    return this.denumire;
}

public void setDenumire(String denumire) {
    this.denumire = denumire;
}
public Integer getCodfiscal() {
    return this.codfiscal;
}

public void setCodfiscal(Integer codfiscal) {
    this.codfiscal = codfiscal;
}
public Integer getNrfirma() {
    return this.nrfirma;
}

public void setNrfirma(Integer nrfirma) {
    this.nrfirma = nrfirma;
}
public String getStradadom() {
    return this.stradadom;
}

public void setStradadom(String stradadom) {
    this.stradadom = stradadom;
}
public Integer getNumardom() {
    return this.numardom;
}

public void setNumardom(Integer numardom) {
    this.numardom = numardom;
}
public String getOrasdom() {
    return this.orasdom;
}

public void setOrasdom(String orasdom) {
    this.orasdom = orasdom;
}
public String getJudetdom() {
    return this.judetdom;
}

public void setJudetdom(String judetdom) {
    this.judetdom = judetdom;
}
public Integer getTelefon() {
    return this.telefon;
}

public void setTelefon(Integer telefon) {
    this.telefon = telefon;
}
public Integer getCodiban() {
    return this.codiban;
}

public void setCodiban(Integer codiban) {
    this.codiban = codiban;
}
public String getBanca() {
    return this.banca;
}

public void setBanca(String banca) {
    this.banca = banca;
}
public Set getProduses() {
    return this.produses;
}

public void setProduses(Set produses) {
    this.produses = produses;
}
public Set getChitantas() {
    return this.chitantas;
}

public void setChitantas(Set chitantas) {
    this.chitantas = chitantas;
}
public Set getComandas() {
    return this.comandas;
}

public void setComandas(Set comandas) {
    this.comandas = comandas;
}
public Set getFacturas() {
    return this.facturas;
}

public void setFacturas(Set facturas) {
    this.facturas = facturas;
}

}

4

2 回答 2

0

为了清楚起见,您的产品与其提供者之间存在多对一的关联。当您插入一个产品时,您希望它将它链接到现有的提供者。因此,您获得了具有给定 ID 的提供程序,并将此提供程序附加到产品:

Provider provider = (Provider) session.load(Provider.class, idOfTheProvider);
product.setProvider(provider);
session.save(product);

其他协会也是如此。

于 2012-05-24T09:53:40.180 回答
-1

以提供者为例。如果您有现有提供者的提供者 ID,则可以执行

  • 首先加载具有 id 的提供程序
  • 将提供者设置到产品中
  • 保存产品

示例(不完整)

Provider provider = session.load(provider, idProvider);
product.setProvider(provider);
session.save(product);

如果您不想先加载提供程序(可能是多余的数据库访问),那么您可以这样做

Provider provider = new Provider(idProvider);
product.setProvider(provider);
session.save(product);

在这种情况下,请确保在映射文件cascade中设置为none; 如果不是也将创建一个新的提供者(如果您还想创建一个新的提供者,请设置cascade=all)。

第三种可能性是,在产品中您不放置提供者对象,而只放置提供者 ID 并将其映射为简单整数。(您甚至可以同时放置单独的 id 和提供者对象,但这有点棘手,通常不推荐。)

于 2012-05-24T10:07:23.417 回答