我在使用 Hibernate ManyToOne 时遇到问题,因为数据库中没有填写外键。
所以我有一个准备班和一个配料班。成分 ID 应该是我的准备类中的外键。
在我的准备课上,我有我的 ManyToOne 注释。当我不设置 nullable=false 时,我可以在我的数据库中填写准备表,但外键保持“NULL”。Wenn 我设置 nullable=false 我得到 - 当然 - 一条错误消息说“非空属性引用空值或瞬态值......”
也许我的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;
}
}
准备道:
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);
}