4

我在使用 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);
            }
4

2 回答 2

0

@rhinds 关于错误所在的位置可能是正确的。

hibernate 模板的文档(来自一个非常旧的 spring 版本,如果您无法控制的东西不需要,恕我直言不应该使用)表明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);
于 2013-06-11T09:38:26.753 回答
0

我通过更改与“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);
}
于 2013-06-22T10:43:07.817 回答