[英]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.