【问题标题】:Many to many doesn't store value in database多对多不在数据库中存储价值
【发布时间】:2014-05-20 14:15:42
【问题描述】:

您好,我遇到了多对多的问题。我有两个课程味道和产品。起初我创造了口味。之后我想从数据库中创建产品并设置品味并保存这个对象。

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

@Entity
public class Taste {

    @Id
    @GeneratedValue
    @Column(name="TASTE_ID")
    private Integer id;
    private String type;
    @ManyToMany(fetch = FetchType.EAGER, mappedBy="tastes")
    private List<Product> products;

    public Taste() {
    }


    public Taste(String type) {
        this.type = type;
    }


    public List<Product> getProducts() {
        return products;
    }
    public void setProducts(List<Product> products) {
        this.products = products;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }


}

二等

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Transient;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

@Entity
public class Product {

    @Id
    @GeneratedValue
    @Column(name="PRODUCT_ID")
    private Integer id;
    @ManyToOne
    private Section section;
    private String name;
    private Integer weight;
    private Double baseCost;
    private Double ourCost;
    @ManyToOne
    private Producer producer;
    private String excerciseType;
    private Integer stockCount;
    private boolean inPackage;
    private String description;
    private Integer soldCount;
    private Double score;
    private Integer scoreCount;
    @OneToMany(fetch = FetchType.EAGER,mappedBy="product")
    private List<Comment> comments;
//  @ManyToOne
    @Transient
    private Order order;
    @OneToMany(mappedBy="product")
    private List<PackageProduct> packageProduct;
    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinTable(name = "product_taste" ,joinColumns = { 
            @JoinColumn(name = "PRODUCT_ID", nullable = false, updatable = false) }, 
            inverseJoinColumns = { @JoinColumn(name = "TASTE_ID", 
                    nullable = false, updatable = false) })
    private List<Taste> tastes;
    private String bigFilePath;
    private String smallFilePath;

    public Product() {
        // TODO Auto-generated constructor stub
    }


    public Product(Section section, String name, Double baseCost,
            Double ourCost, Producer producer, String excerciseType,
            Integer stockCount, boolean inPackage, String description,
            Integer soldCount, Double score, Integer scoreCount,
            List<Taste> tastes) {
        this.section = section;
        this.name = name;
        this.baseCost = baseCost;
        this.ourCost = ourCost;
        this.producer = producer;
        this.excerciseType = excerciseType;
        this.stockCount = stockCount;
        this.inPackage = inPackage;
        this.description = description;
        this.soldCount = soldCount;
        this.score = score;
        this.scoreCount = scoreCount;
        this.tastes = tastes;
    }


    public Order getOrder() {
        return order;
    }
    public void setOrder(Order order) {
        this.order = order;
    }

    public List<PackageProduct> getPackageProduct() {
        return packageProduct;
    }


    public void setPackageProduct(List<PackageProduct> packageProduct) {
        this.packageProduct = packageProduct;
    }


    public List<Taste> getTastes() {
        return tastes;
    }
    public void setTastes(List<Taste> tastes) {
        this.tastes = tastes;
    }
    public List<Comment> getComments() {
        return comments;
    }
    public void setComments(List<Comment> comments) {
        this.comments = comments;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public Section getSection() {
        return section;
    }
    public void setSection(Section section) {
        this.section = section;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Double getBaseCost() {
        return baseCost;
    }
    public void setBaseCost(Double baseCost) {
        this.baseCost = baseCost;
    }
    public Double getOurCost() {
        return ourCost;
    }
    public void setOurCost(Double ourCost) {
        this.ourCost = ourCost;
    }
    public Producer getProducer() {
        return producer;
    }
    public void setProducer(Producer producer) {
        this.producer = producer;
    }
    public Integer getStockCount() {
        return stockCount;
    }
    public void setStockCount(Integer stockCount) {
        this.stockCount = stockCount;
    }
    public boolean isInPackage() {
        return inPackage;
    }
    public void setInPackage(boolean inPackage) {
        this.inPackage = inPackage;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public Integer getSoldCount() {
        return soldCount;
    }
    public void setSoldCount(Integer soldCount) {
        this.soldCount = soldCount;
    }
    public Double getScore() {
        return score;
    }
    public void setScore(Double score) {
        this.score = score;
    }
    public Integer getScoreCount() {
        return scoreCount;
    }
    public void setScoreCount(Integer scoreCount) {
        this.scoreCount = scoreCount;
    }


    public String getExcerciseType() {
        return excerciseType;
    }


    public void setExcerciseType(String excerciseType) {
        this.excerciseType = excerciseType;
    }


    public Integer getWeight() {
        return weight;
    }


    public void setWeight(Integer weight) {
        this.weight = weight;
    }


    public String getBigFilePath() {
        return bigFilePath;
    }


    public void setBigFilePath(String bigFilePath) {
        this.bigFilePath = bigFilePath;
    }


    public String getSmallFilePath() {
        return smallFilePath;
    }


    public void setSmallFilePath(String smallFilePath) {
        this.smallFilePath = smallFilePath;
    }


}

道之法,我尽皆知

@Override
    public List<Taste> getAllTastesByIds(List<Integer> ids) {
        Session s = null;
        s = sessionFactory.openSession();
        String hql = "FROM Taste where id IN(:ids)";
        Query query = s.createQuery(hql);
        query.setParameterList("ids", ids);
        List<Taste> results = (List<Taste>) query.list();
        s.close();
        return results;
    }

之后我想保存我的产品

@Override
    public void addNewProduct(Product object) {
        Session s = null;
        s = sessionFactory.openSession();
        s.saveOrUpdate(object);
        s.close();

    }

但数据库中没有记录,所以当我想列出所有产品及其口味时,我无法获得它:(

从休眠中记录

休眠: 插入 进入 产品 (baseCost, bigFilePath, description, excerciseType, inPackage, name, ourCost, producer_id, score, scoreCount, section_id, smallFilePath、soldCount、stockCount、重量) 价值观 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

我尝试了另一个原因,更新 Taste 并将它们设置为所有产品

@Override
public void updateTaste(Taste object) {
    Session s = null;
    s = sessionFactory.openSession();
    String hql = "UPDATE Taste set type= :type, products = :products where id = :id";
    Query query = s.createQuery(hql);
    query.setParameter("type", object.getType());
    query.setParameterList("products", object.getProducts());
    query.setParameter("id", object.getId());
    query.executeUpdate();
    s.close();

}

但我得到错误

Hibernate: 
    update
        Taste cross 
    join

    set
        type=?,
        {non-qualified-property-ref}=? 
    where
        TASTE_ID=?
WARN : org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 0, SQLState: 07001
ERROR: org.hibernate.engine.jdbc.spi.SqlExceptionHelper - No value specified for parameter 3

【问题讨论】:

    标签: hibernate session spring-mvc


    【解决方案1】:

    初步看来,它似乎需要“描述”字段的数据。您能否确认description 是 NOTNULL 并且其中已经存在一个值?

    如果可能,请在您的表格设置中粘贴(指出字段以及哪些字段为 NOTNULL,哪些字段为 NULLABLE)。

    如果以上不是答案,我会为您进一步调查。

    【讨论】:

      猜你喜欢
      • 2021-12-02
      • 1970-01-01
      • 2020-11-28
      • 2010-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-26
      • 2013-07-17
      相关资源
      最近更新 更多