您好我有很多问题。我有两个课程品尝和产品。起初我创造了品味。之后我想创建pruduct并设置DB的味道并保存这个对象。
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;
}
}
来自dao的方法,我会得到最好的一切
@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();
}
但数据库中没有记录,所以当我想列出所有产品和他们的口味时,我无法得到它:(
从hibernate登录
休眠: 插入 成 产品 (baseCost,bigFilePath,description,excerciseType,inPackage,name,ourCost,producer_id,score,scoreCount,section_id, smallFilePath,soldCount,stockCount,weight) 值 (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
我尝试了另一个原因,更新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
答案 0 :(得分:0)
从初步看起来,它似乎需要数据来描述&#39;领域。您能否确认description
是否已经过去并且其中已存在值?
如果可能的话,请粘贴到您的表格设置中(指明字段和哪些是NOTNULL以及哪些是NULLABLE)。
如果以上不是答案,我会进一步调查你。