[英]HIBERNATE - ManyToOne - Not-null property references a null or transient value
我有Hibernate ManyToOne的問題,因為數據庫中沒有填寫外鍵。
所以我有一個准備課和一個成分課。 ingredientsId應該是我的預備課程中的外鍵。
在我的Prepartion課程中,我有我的ManyToOne Annotation。 當我沒有設置nullable = false時,我能夠填充我的數據庫中的准備表,但外鍵保持“NULL”。 Wenn我設置nullable = false我得到 - 當然 - 一條錯誤消息說“Not-null屬性引用了null或瞬態值......”
也許我的PreparationDao也有問題......
我真的不知道我做錯了什么。 我希望你能幫助我。
提前感謝並提前對不起,因為這是我的第一個問題(在閱讀了關於如何問我的指南后我幾乎不敢問:D)我的問題可能也很愚蠢,但我是一個絕對的初學者!
我的配料類:
@Entity
@Table(name="tbl_ingredients")
public class Ingredients {
@Id
@GeneratedValue
@Column(name="ingredients_id")
private Integer id;
private String name;
private float percent;
// @OneToMany(mappedBy="ingredients", cascade=CascadeType.ALL)
// private Set<Preparation> preparation = new HashSet<Preparation>();
//
//
// public Set<Preparation> getPreparation() {
// return preparation;
// }
//
// public void setPreparation(Set<Preparation> preparation) {
// this.preparation = preparation;
// }
//Konstrukturmethode der Klasse
public Ingredients(){
}
public Ingredients(Integer id, String name, float percent){
this(name,percent);
setId(id);
}
public Ingredients(String name, float percent){
setName(name);
setPercent(percent);
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPercent() {
return percent;
}
public void setPercent(float percent) {
this.percent = percent;
}
}
我的准備課程:
@Entity
@Table(name="tbl_preparation")
public class Preparation {
@Id
@GeneratedValue
@Column(name="preparation_id")
private Integer id;
private int amount;
private String bevvalue;
@ManyToOne (optional = false, fetch = FetchType.LAZY)
@JoinColumn(name="ingredients_id", nullable=false)
private Ingredients ingredients;
@OneToMany(mappedBy="preparation")
private Set<Cocktail> cocktail = new HashSet<Cocktail>();
// Getter und Setter Methoden
public Preparation(){
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public Ingredients getIngredients() {
return ingredients;
}
public void setIngredients(Ingredients ingredients) {
this.ingredients = ingredients;
}
public String getBevvalue() {
return bevvalue;
}
public void setBevvalue(String bevvalue) {
this.bevvalue = bevvalue;
}
public Set<Cocktail> getCocktail() {
return cocktail;
}
public void setCocktail(Set<Cocktail> cocktail) {
this.cocktail = cocktail;
}
}
PreparationDao:
public class PreparationDao extends HibernateDaoSupport {
// Methode zum Anlegen der Zubereitung in der Datenbank
// Methode zum Speichern der Zubereitung
public Preparation save(Preparation preparation) {
HibernateTemplate template = getHibernateTemplate();
template.saveOrUpdate(preparation);
return preparation;
}
// ing cocktailid muss wieder eingetragen werden
public void create(Integer [] ingredientsid, int amount, String bevvalue){
// Set<Cocktail> cocktail = new HashSet<Cocktail>();
HibernateTemplate template = getHibernateTemplate();
Ingredients ingredients = (Ingredients) template.get(Ingredients.class, ingredientsid);
Preparation p = new Preparation();
// p.setCocktail(cocktail);
p.setIngredients(ingredients);
p.setAmount(amount);
p.setBevvalue(bevvalue);
template.saveOrUpdate(p);
}
@rhinds可能是錯誤所在的位置。
hibernate模板的文檔 (來自一個非常古老的spring版本和imho不應該被你控制之外的東西使用),這表明HibernateTemplate#get()
只接受標識符數組但是設計了為單個標識符/返回對象工作。
因此,要獲得一個成分列表,您必須迭代列表並逐個獲取成分:
HibernateTemplate template = getHibernateTemplate();
Ingredients[] ingredients;
int i = 0;
for (int currentid: ingredientsid){
ingredients[i] = (Ingredients) template.get(Ingredients.class, currentid);
i++
}
Preparation p = new Preparation();
// p.setCocktail(cocktail);
p.setIngredients(ingredients);
我通過改變我與“ManyToMany”的關系並調整我的創建方法解決了這個問題......這就是它現在的外觀和工作方式:
我的准備課程:
import java.util.HashSet;
import java.util.Set;
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.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="tbl_preparation")
public class Preparation {
@Id
@GeneratedValue
@Column(name="preparation_id")
private Integer id;
private String prepdesc;
@ManyToMany(targetEntity=de.hdu.pms.model.Ingredients.class,
cascade={CascadeType.PERSIST},
fetch = FetchType.LAZY)
@JoinTable(
name="ingredientstopreparation",
joinColumns={@JoinColumn(name="fk_preparation")},
inverseJoinColumns={@JoinColumn(name="fk_ingredients")})
private Set<Ingredients> ingredients;
@ManyToOne
@JoinColumn(name="fk_cocktail", nullable=false)
private Cocktail cocktail;
我的准備道:
public void create(Integer[] ingredientsnr, Integer cocktailid, String prepdesc){
HibernateTemplate template = getHibernateTemplate();
Cocktail cocktail = (Cocktail) template.get(Cocktail.class, cocktailid);
Set<Ingredients> ingredients = new HashSet<Ingredients>();
for (int ingredientsid : ingredientsnr){
ingredients.add((Ingredients) template.get(Ingredients.class, ingredientsid));
}
Preparation p = new Preparation();
p.setCocktail(cocktail);
p.setIngredients(ingredients);
p.setPrepdesc(prepdesc);
template.saveOrUpdate(p);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.